[英]Cannot implicitly convert type 'System.Linq.IQueryable<AnonymousType#1>'
Im getting an error in my C# project which is causing me a headache. 我在C#项目中遇到错误,这让我很头疼。 The error is:
错误是:
Cannot implicitly convert type 'System.Linq.IQueryable<AnonymousType#1>'
to System.Collections.Generic.IEnumerable<KST.ViewModels.Gallery>'.
Here's my LINQ querys: 这是我的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 };
Here's my viewmodels. 这是我的视图模型。
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; }
}
The squigally line error occurs under GalleryResults: 在GalleryResults下发生了斜线错误:
//Create my viewmodel
var Model = new GalleryViewModel
{
Media = PictureResults,
Gallery = GalleryResults
};
Ufuk Hacıoğulları has posted an answer and deleted it a few minutes later. UfukHacıoğulları已经发布了一个答案并在几分钟后删除了它。 I think his answer was correct and his solution gets rid of the error message.
我认为他的回答是正确的,他的解决方案摆脱了错误信息。 So I'm posting it again:
所以我再次发布它:
Ufuk Hacıoğulları's answer; UfukHacıoğulları的回答;
You are projecting a sequence of anonymous type instead of Gallery
. 您正在投射一系列匿名类型而不是
Gallery
。 Just instantiate Gallery objects in your select statement and it should work. 只需在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 };
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.