2011-11-15 24 views
2

我的LINQ查詢如下動態LINQ。無屬性或字段「字段名」式存在「類名」

Dim Query = From t In New XPQuery(Of xUser)(Xpo.Session.DefaultSession) 
.Where("Name=John").Select("new (Name as FirstName)") 

不幸的是我得到的錯誤No property or field 'John' exists in type 'xUser'

當然沒有這樣的屬性在我的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 

如何編輯我的「故障」查詢?我在這裏做錯了什麼?

謝謝你的時間。

+0

如果您嘗試將'John'設置爲參數而不是直接在字符串中,該怎麼辦? –

+0

@ Wouter de Kort It works!你可以把它作爲答案嗎? – OrElse

回答

7

嘗試在字符串中設置「約翰」作爲參數,而不是直接。

Here你可以找到一些文件顯示這一點。它看起來像.Where("[email protected]", "John")

4

它應該是.Where("Name='John'")(引號圍繞字符串)。

或者,你可以使用一個參數:.Where("[email protected]", "John")