gpt4 book ai didi

c# - 无法隐式转换类型 'System.Linq.IQueryable'

转载 作者:行者123 更新时间:2023-11-30 14:13:29 25 4
gpt4 key购买 nike

我的 C# 项目中出现错误,这让我很头疼。错误是:

Cannot implicitly convert type 'System.Linq.IQueryable<AnonymousType#1>'
to System.Collections.Generic.IEnumerable<KST.ViewModels.Gallery>'.

这是我的 LINQ 查询:

//Get Single Picture
var PictureResults = (from m in DB.Media where m.MediaID == 450 select m).SingleOrDefault();

//Get Gallery Pictures and Gallery Title
var GalleryResults = from g in DB.Galleries
join m in DB.Media on g.GalleryID equals m.GalleryID into gm
where g.GalleryID == 100
select new { g.GalleryTitle, Media = gm };

这是我的 View 模型。

public class GalleryViewModel
{
public Media Media { get; set; }
public IEnumerable<Gallery> Gallery { get; set; }
}

public class Gallery
{
public string GalleryTitle { get; set; }
public int MediaID { get; set; }
public int GalleryID { get; set; }
public string MediaGenre { get; set; }
public string MediaTitle { get; set; }
public string MediaDesc { get; set; }
}

GalleryResults下出现波浪线错误:

//Create my viewmodel
var Model = new GalleryViewModel
{
Media = PictureResults,
Gallery = GalleryResults
};

最佳答案

Ufuk Hacıoğulları 发布了一个答案并在几分钟后将其删除。我认为他的回答是正确的,他的解决方案摆脱了错误信息。所以我再次发布:

Ufuk Hacıoğulları 的回答;

您正在投影一系列匿名类型而不是 Gallery。只需在您的 select 语句中实例化 Gallery 对象,它就会起作用。

var GalleryResults = from g in DB.Galleries
join m in DB.Media on g.GalleryID equals m.GalleryID into gm
where g.GalleryID == 100
select new Gallery { GalleryTitle = g.GalleryTitle, Media = gm };

关于c# - 无法隐式转换类型 'System.Linq.IQueryable<AnonymousType#1>',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14129147/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com