ASP.NET实体框架NotSupportedException

Jas*_*son 2 c# asp.net linq-to-entities

我在我的应用程序的数据层中使用LINQ to Entities,但是在调用results.ToList()时遇到了NotSupportedException.这是导致异常的函数:

    public List<Organization> GetByLocation(Location l)
    {
        using (Entities entities = new Entities())
        {
            var results = from o in entities.OrganizationSet
                          where o.Location == l
                          select o;

            return results.ToList<Organization>();
        }
    }
Run Code Online (Sandbox Code Playgroud)

关键是将给定位置的所有组织的列表返回到服务层(将其返回给MVC控制器,MVC控制器将其转换为JSON,然后将其返回给客户端).服务层期望返回List.

这可能很简单......任何帮助?

Cra*_*ntz 8

public List<Organization> GetByLocation(Location l)
{
    using (Entities entities = new Entities())
    {
        var results = from o in entities.OrganizationSet
                      where o.Location.Id == l.Id
                      select o;

        return results.ToList<Organization>();
    }
}
Run Code Online (Sandbox Code Playgroud)

由于此查询将转换为SQL,因此无法进行参考比较l.相比之下,比较PK.