LINQ is dynamic. The type 'ClassName' has no 'FieldName' property or field.

dynamic-linq linq vb.net xpo

Question

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.

1
2
11/15/2011 9:07:46 AM

Accepted Answer

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

7
11/15/2011 9:20:26 AM

Popular Answer

It should be .Where("Name='John'") (quotes around the string).

Alternatively, you can use a parameter: .Where("Name=@0", "John")



Related Questions





Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow