Tôi có một truy vấn LINQ trả về kết quả phù hợp với lớp học PictureGallery
của tôi. Tôi cần tải chúng vào số ViewModel
nhưng tôi nhận được lỗi sau:Làm cách nào để tải các kết quả LINQ này vào lớp ViewModel của tôi?
Cannot implicitly convert type 'System.Linq.IQueryable' to 'System.Collections.Generic.IEnumerable'. An explicit conversion exists (are you missing a cast?)
Tôi khá mới ở C#. Làm cách nào để truyền "Kết quả" vào lớp xem hình ảnh "PictureGallery" của tôi?
Cảm ơn trước!
Bộ điều khiển:
//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()
};
ViewModels:
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; }
}
Yup mà làm việc! Cảm ơn – Maddhacker24