English 中文(简体)
Returning Custom Classes as IQueryable so doesn t have has no supported translation to SQL error
原标题:

I have the below call in my repository where I return IQueryable of Node (my business object class) then I have a filter function in my service layer which adds to the IQueryable call and filters the repository function GetNodes by id. When I return the FilterById as a list (so it executes) I get error -- The member bo.Node.Id has no supported translation to SQL. --

Is there a way to get around this error when returning a not linq generated class as 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));
}

In my service layer

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

Don t use Node constructor, instead use property initialization:

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 by using the constructor you don t give any info in the expression of what properties match others. Besides the resulting instance is based on whatever logic is in the constructor, so there is no any valid assumption linq can make about those ... other than just call the constructor when returning the instance of it.

问题回答

暂无回答




相关问题
IQueryable into a hierarchy

I currently have an IQueryable of Questions. In my Question object I have and "id" and a "parentId" which can be used to create a hierarchy. Currently, I bind a RadTreeView to the IQueryable of ...

sorting of an Iqueryable

I have an IQueryable with duplicate entries and I want to sort this IQueryable by the count of occurrences.

Determine the position of an element in an IQueryable

I have a IQueryable which is ordered by some condition. Now I want to know the position of a particular element in that IQueryable. Is there a linq expression to get that. Say for example there are ...

Linq-Sql IQueryable<T> and chaining OR operations

I m trying to simulate: WHERE x.IsActive = true OR x.Id = 5 The following causes AND to be used... how do I simulate an OR condition with IQueryable (qry) and my nullable int, given that other ...

Linq, Where Extension Method, Lambda Expressions, and Bool s

Greetings, I am having some issues with using a bool operation within a Where clause extension method of an IQueryable object obtained using Linq to Entities. The first example is showing what does ...

Linq - How to turn IQueryable<IEnumerable> into IQueryable

I have a simple linq statement that isn t quite returning what I would like. I understand why, I just don t know how to wriet it to get what I want. The query is as follows: answers = from a in ents....

热门标签