开发者

Linq to Entities, checking for specific foreign key id?

I am trying to get rows where the foreign key ParentID == 0, and this is what I am trying but I get a NotSupportedException because it can't translate the ArrayIndex [0]:

IQueryable<ApplicationSectionNode> topLevelNodeQuery =
    from n in uacEntitiesContext.ApplicationSectionNodeSet
    where (int)n.Parent.EntityKey.EntityKeyValues[0].Value == 0
    orderby n.Sequence
    select n;

So I need to pull that ArrayIndex out of the query so that the runtime can successfully translate the query. I'm not sure how to do that though. How does one query a specific object via it's primary key or set of objects via foreign key?

Edit: Note that there is not actually a row in the table with NodeId == 0, the 0 is a magic value(not my idea) to indicate top level nodes. So I can't d开发者_运维百科o n.Parent.NodeId == 0


You should be able to use where n.Parent == null. The reason that works is that EF can't find any row with the ID of 0 in the database, so instead it sets the property to null (and you can query it in the same manner).


What about:

IQueryable<ApplicationSectionNode> topLevelNodeQuery =
from n in uacEntitiesContext.ApplicationSectionNodeSet
where (int)n.Parent.EntityKey.EntityKeyValues.First().Value == 0
orderby n.Sequence
select n;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜