【发布时间】:2012-12-19 10:17:37
【问题描述】:
我有一个 LINQ 查询,它返回与我的 PictureGallery 类匹配的结果。我需要将它们加载到我的ViewModel 但我收到以下错误:
不能隐式转换类型 'System.Linq.IQueryable' 到 'System.Collections.Generic.IEnumerable'。 存在显式转换(您是否缺少演员表?)
我是 C# 的新手。如何将“结果”投射到我的“PictureGallery”viewmddel 类中?
提前致谢!
控制器:
//Test MediaID
var MediaID = 75;
//Query Results
var Results = from g in DB.Galleries
join m in DB.Media on g.GalleryID equals m.GalleryID
where g.GalleryID == GalleryID
orderby m.MediaDate descending, m.MediaID descending
select new { g.GalleryTitle, Media = m };
//Create my viewmodel
var Model = new GalleryViewModel
{
MediaID = MediaID,
PictureGallery = Results, //This line throws the error.
PictureCount = Results.Count()
};
视图模型:
public class GalleryViewModel
{
public int? MediaID { get; set; }
public IEnumerable<PictureGallery> PictureGallery { get; set; }
public int PictureCount { get; set; }
}
public class PictureGallery
{
public int GalleryID { get; set; }
public string GalleryTitle { get; set; }
public int MediaID { get; set; }
public string MediaTitle { get; set; }
public string MediaDesc { get; set; }
public double Rating { get; set; }
public int Views { get; set; }
}
【问题讨论】:
标签: c# asp.net asp.net-mvc asp.net-mvc-3 linq