动态 LINQ。没有属性或字段“FieldName”;存在于类型“ClassName”中

发布于 2024-12-15 17:36:47 字数 964 浏览 3 评论 0原文

我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦巷 2024-12-22 17:36:47

尝试将“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")

江南月 2024-12-22 17:36:47

它应该是 .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")

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文