Linq to sql 将自定义类返回为IQueryable,因此没有“不支持转换为SQL”错误

Linq to sql 将自定义类返回为IQueryable,因此没有“不支持转换为SQL”错误,linq-to-sql,repository,iqueryable,Linq To Sql,Repository,Iqueryable,我在我的存储库中有下面的调用,其中我返回我的业务对象类的IQueryable节点,然后我在我的服务层中有一个filter函数,它添加到IQueryable调用中,并根据id过滤存储库函数GetNodes。当我将FilterById作为列表返回以便执行时,我得到错误-成员“bo.Node.id”不受支持翻译成SQL 当将非linq生成的类返回为IQueryable时,是否有办法避免此错误 public IQueryable<bo.Node> GetNodes() { re

我在我的存储库中有下面的调用,其中我返回我的业务对象类的IQueryable节点,然后我在我的服务层中有一个filter函数,它添加到IQueryable调用中,并根据id过滤存储库函数GetNodes。当我将FilterById作为列表返回以便执行时,我得到错误-成员“bo.Node.id”不受支持翻译成SQL

当将非linq生成的类返回为IQueryable时,是否有办法避免此错误

public IQueryable<bo.Node> GetNodes()
{
       return (from item in Db.Nodes select new bo.Node(item.id, 
              item.created, item.lastupdated, item.name, 
              item.parentid, item.siteid, item.userid));
}
在我的服务层

private IQueryable<bo.Node> FilterById(IQueryable<bo.Node> source, Guid id)
{
       return source.Where(i => i.Id.Equals(id))
}

不要使用节点构造函数,而是使用属性初始化:

return (
   from item in Db.Nodes select new bo.Node{
      item.id, item.created, item.lastupdated, item.name, 
      item.parentid, item.siteid, item.userid
   });

afaik通过使用构造函数,您不会在表达式中提供任何与其他属性匹配的信息。此外,生成的实例基于构造函数中的任何逻辑,因此linq无法对这些逻辑做出任何有效的假设。。。除了在返回构造函数实例时调用构造函数之外。

可能不会。为什么不直接返回Db.Nodes呢?太好了,真不敢相信这么简单