2011-07-28 19 views
1

以下のコードはコンパイルされません。コンパイラエラーはクエリ結果を汎用リストに格納する方法

Cannot implicitly convert type 'System.Collections.Generic.List<AnonymousType#1>' to 'System.Collections.Generic.List<AnimeVoter.Models.MyTitleObject>' d:\dev\mvc\AnimeVoter\AnimeVoter\Controllers\SearchController.cs 

です。以下はコードです。 MyTitleObjectは私が達成しようとしているものを要約すると

public class MyTitleObject 
{ 
    public string Name { get; set; } 
    public string Description { get; set; } 
    public string UrlImage { get; set; } 
    public int Votes { get; set; } 
} 

として定義されて

[HttpPost] 
public ActionResult Search(FormCollection collection) 
{ 
    string filter = collection["search_fld"]; 

    List<MyTitleObject> list = 
     (
     from O in db.Titles 
     join K in db.TitleDescriptions on O.Id equals K.TitleId 
     where O.Name.ToLower() == filter.ToLower() 
     select new { Name = O.Name, Description = K.Description, UrlImage = O.UrlImage, Votes = O.Votes } 
     ).ToList(); // this is where the error points at 

    return View(list); 
} 

ビューに結果のリストを送信することです。

答えて

2

あなただけの選択ライン上のタイプを指定する必要があります。

select new MyTitleObject { Name = O.Name, Description = K.Description, UrlImage = O.UrlImage, Votes = O.Votes } 
+0

素晴らしい仕事!ありがとう。 – Ronald

関連する問題