2011-11-08 149 views
1

我有一個函數返回一個JsonResult,它應該是與所提供的guid關聯的所有對象類型的列表。但我得到錯誤linq返回關聯的對象類型

LINQ to Entities不識別方法'System.Type GetType()'方法,並且此方法不能轉換爲存儲表達式。

這可能嗎?

public JsonResult GetWebObjectTypesByWebObject(Guid id) 
{ 
    JsonResult result = new JsonResult(); 
    var resultData = (from w in db.WebObjects 
         from r in w.RelatedWebObjects 
         where w.Id == id 
         select new { Type = r.GetType().BaseType.Name }); 
    result.Data = resultData; 
    result.JsonRequestBehavior = JsonRequestBehavior.AllowGet; 
    return result; 
} 
+2

可能是另一個'ToList'掛斷 - 可能試着強迫你的結果進入列表。 –

+0

同意,ToList然後選擇。 – maxbeaudoin

+0

仍然會出現相同的錯誤。啊,我明白了,tolist然後選擇。讓我嘗試一下。 – bflemi3

回答

0

試試這個:

public JsonResult GetWebObjectTypesByWebObject(Guid id) 
{ 
    JsonResult result = new JsonResult(); 
    var data = (from w in db.WebObjects 
         from r in w.RelatedWebObjects 
         where w.Id == id 
         select r).ToList(); 
    var resultData = data.Select(r => new { Type = r.GetType().BaseType.Name }); 
    result.Data = resultData; 
    result.JsonRequestBehavior = JsonRequestBehavior.AllowGet; 
    return result; 
} 

你失去了延遲執行,但在這種情況下,似乎無關緊要。