2013-01-02 107 views
2

在我的C#項目中遇到了一個錯誤,導致我頭痛。錯誤是:無法隱式轉換類型'System.Linq.IQueryable <AnonymousType#1>'

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

這裏是我的LINQ querys:

//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 }; 

這裏是我的ViewModels。下GalleryResults發生

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; } 
} 

的squigally行錯誤:

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

錯誤信息的哪部分你不明白? – SLaks

回答

6

UfukHacıoğulları已經發布了一個答案,刪除了它,幾分鐘後。我認爲他的回答是正確的,他的解決方案擺脫了錯誤信息。所以我再次張貼它:

UfukHacıoğulları's answer;

您正在投射一系列匿名類型而不是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 }; 
+0

如果新的畫廊=從我的畫廊類從上面? – Maddhacker24

+0

是的。從您刪除的答案看起來好像您沒有導入所需的名稱空間。所以你需要提供完整的類名:'選擇新的KST.ViewModels.Gallery {GalleryTitle = g.GalleryTitle,Media = gm}'。 – Codo

相關問題