linq返回关联的对象类型

时间:2011-11-08 14:15:57

标签: linq entity-framework linq-to-entities

我有一个返回JsonResult的函数,它应该是与提供的guid相关联的所有对象类型的列表。但是我收到了错误

  

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

1 个答案:

答案 0 :(得分: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;
}

你放弃了延期执行,但在这种情况下似乎无关紧要。