我在我的 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 };
这是我的视图模型。
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
};