动态 LINQ。没有属性或字段“FieldName”;存在于类型“ClassName”中
我的 LINQ 查询如下
Dim Query = From t In New XPQuery(Of xUser)(Xpo.Session.DefaultSession)
.Where("Name=John").Select("new (Name as FirstName)")
不幸的是我收到错误类型'xUser'中不存在任何属性或字段'John'
当然我的xUser类中不存在这样的属性,但是我可以解决这个问题吗?
在阅读 DynamicLinq 类后,我发现了这个函数
Function FindPropertyOrField(ByVal type As Type, ByVal memberName As String, ByVal staticAccess As Boolean) As MemberInfo
Dim flags As BindingFlags = BindingFlags.Public Or BindingFlags.DeclaredOnly Or _
If(staticAccess, BindingFlags.Static, BindingFlags.Instance)
For Each t As Type In SelfAndBaseTypes(Type)
Dim members As MemberInfo() = t.FindMembers(MemberTypes.Property Or MemberTypes.Field, _
flags, type.FilterNameIgnoreCase, memberName)
If members.Length <> 0 Then Return members(0)
Next
Return Nothing
End Function
我如何编辑我的“错误”查询?我在这里做错了什么?
感谢您抽出时间。
My LINQ query is as follows
Dim Query = From t In New XPQuery(Of xUser)(Xpo.Session.DefaultSession)
.Where("Name=John").Select("new (Name as FirstName)")
Unfortunately i get the error No property or field 'John' exists in type 'xUser'
Of course no such property exists in my xUser class, but hot can i fix that?
After reading within the DynamicLinq Class i found this function
Function FindPropertyOrField(ByVal type As Type, ByVal memberName As String, ByVal staticAccess As Boolean) As MemberInfo
Dim flags As BindingFlags = BindingFlags.Public Or BindingFlags.DeclaredOnly Or _
If(staticAccess, BindingFlags.Static, BindingFlags.Instance)
For Each t As Type In SelfAndBaseTypes(Type)
Dim members As MemberInfo() = t.FindMembers(MemberTypes.Property Or MemberTypes.Field, _
flags, type.FilterNameIgnoreCase, memberName)
If members.Length <> 0 Then Return members(0)
Next
Return Nothing
End Function
How can i edit my "faulty" query? What am i doing wrong here?
Thanks for your time.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试将“John”设置为参数,而不是直接在字符串中设置。
在这里您可以找到一些说明这一点的文档。它看起来像
.Where("Name=@0", "John")
Try to set 'John' as a parameter instead of directly in the string.
Here you can find some documentation which shows this. It would look like
.Where("Name=@0", "John")
它应该是
.Where("Name='John'")
(字符串周围的引号)。或者,您可以使用参数:
.Where("Name=@0", "John")
It should be
.Where("Name='John'")
(quotes around the string).Alternatively, you can use a parameter:
.Where("Name=@0", "John")