以下 Where
子句中无法识别实体字段。 VB错了吗?
Dim projects = context.projects
.OrderBy(Function(x) x.name)
.Select(Function(x) {x.id, x.name})
.Where(Function(x) x.id <> sourceid)
如果我去掉 Where
,它就可以正常工作。另外,如果我翻转 Where
和 OrderBy
,Where
没问题,但现在 OrderBy
失败了。
最佳答案
试试这个:
Dim projects = context.projects
.OrderBy(Function(x) x.name)
.Select(Function(x) New With {x.id, x.name})
.Where(Function(x) x.id <> sourceid)
New With
关键字应创建匿名类型的 IEnumerable
。然后,您可以在 Where
子句中使用 id
属性,而无需更改操作顺序。
没有什么可以阻止您按顺序执行 OrderBy
、Select
、Where
操作。上面的代码肯定能编译并运行。但是,从逻辑上讲,您需要在 Select
之前执行 Where
,因为前者是过滤操作,而后者是投影操作。
关于vb.net - vb.net 无法识别 LINQ 查询中的 where 子句?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24888911/