2010-05-24 69 views
0

我想获得其中的外键PARENTID == 0,这就是我想要的行,但我得到NotSupportedException异常,因为它无法翻译ArrayIndex [0]Linq to Entities,检查特定的外键ID?

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

所以我需要将该ArrayIndex拉出查询,以便运行时可以成功转换查询。我不知道该怎么做。如何通过外键通过主键或对象组来查询特定对象?

编辑:请注意,表中实际上没有一行中的NodeId == 0,0是一个魔术值(不是我的想法)来指示顶级节点。所以我不能做n.Parent.NodeId == 0

+2

你试过“哪里n.Parent == null”? – 2010-05-24 16:45:24

+0

@Mattias实际上有效。我不认为这是因为制作数据库的人使用的值为0而不是null。请将此作为答案发布,我会将其标记为已接受。 – AaronLS 2010-05-24 17:21:32

回答

1

您应该能够使用where n.Parent == null。原因是EF无法在数据库中找到任何ID为0的行,因此它将该属性设置为null(并且可以用相同的方式查询它)。

1

关于什么:

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

不错的尝试。我尝试过类似的变化。您的代码会生成新的NotSupportedException消息:LINQ to Entities不支持指定的类型成员“EntityKey”。仅支持初始化程序,实体成员和实体导航属性。 – AaronLS 2010-05-24 17:13:32