2012-01-26 87 views
3

我試圖使用LINQ/LAMBDA訪問屬性無法解析屬性(LINQ /λ)

List<Latest> latestBooks = DataContext.Session.Query<Book>().Where(x=> x.Enabled == "True").Select(x => new Latest(x.Title,x.Author)).Take(10).ToList(); 

但有看起來像這樣的書類中定義的另一個屬性:

public virtual string FrontEndLink { get { return string.Format("http://myurl/{0}", Filename); } } 

當我嘗試這個

List<Latest> latestBooks = DataContext.Session.Query<Book>().Where(x=> x.Enabled == "True").Select(x => new Latest(x.FrontEndLink)).Take(10).ToList(); 

代碼休息,並給我一個錯誤:

Could not resolve property: Quote((x,) => (x.Id)),), Quote((x,) => (new Latest(x.FrontEndLink,))),), p1,)] 

回答

3

LINQ to Entities試圖將ToList之前的所有內容轉換爲SQL - 並且無法使用您的FrontEndLink屬性來完成此操作。你想在LINQ to Objects中做那一點,但之後只能篩選出10個結果。所以我會用:

var latestBooks = DataContext.Session.Query<Book>() 
          // Do the first bit in the database 
          .Where(x=> x.Enabled == "True") 
          .Take(10) 
          // Do the rest in LINQ to Objects 
          .AsEnumerable() 
          .Select(x => new Latest(x.FrontEndLink)) 
          .ToList(); 
+1

非常感謝喬恩的解決方案和明確的解釋... thnx –

相關問題