views:

49

answers:

2

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 do n.Parent.NodeId == 0

+1  A: 

What about:

IQueryable<ApplicationSectionNode> topLevelNodeQuery =
from n in uacEntitiesContext.ApplicationSectionNodeSet
where (int)n.Parent.EntityKey.EntityKeyValues.First().Value == 0
orderby n.Sequence
select n;
Ben Robinson
Nice try. I tried similar variations. Your code produces a new NotSupportedException message: The specified type member 'EntityKey' is not supported in LINQ to Entities. Only initializers, entity members, and entity navigation properties are supported.
AaronLS
+1  A: 

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).

Mattias Jakobsson