2011-11-08 105 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; 
} 

你失去了延迟执行,但在这种情况下,似乎无关紧要。