开发者

Accessing anonymous types returned via a dynamic link query

I have been trying to access the data returned from a dynamic linq query as an anonymous type. The advice I have found suggests that I should create a custom type and use it in the Select clause with the new keyword. I was directed to the followed Question for a code example:

System.LINQ.Dynamic: Select(" new (...)") into a List<T> (or any other enumerable collection of <T>)

This was indeed an excellent example which I incorporated into my code (which is VB so I had to do some translation).

My code compiles fine but When I run it , I get the error following error:

"Value cannot be null. Parameter name: member" at the following line from the example: bindings[i] = Expression.Bind(type.GetProperty(properties[i].Name), expressions[i]);

This seems to be linked to expressions(i), which correctly contains two items as I am returning two fields from the database table. properties(i) holds the name of those two fields correctly. Any ideas as to what the value for member is supposed to be and where it should be found? Is it something from the database?

The where clause of this query works and when I run it as an anonymous type it brings back records (or rather two fields from records). The returned fields contain data not nulls.

Here is my VB version of the code from the example provided in the earlier Question. I have bolded or ** the line where the error occurs. Any ideas as to what is causing this?

Much appreciated.

Function ParseNew() As Expression
        NextToken()
        ValidateToken(TokenId.OpenParen, Res.OpenParenExpected)
        NextToken()
        Dim properties As New List(Of DynamicProperty)()
        Dim expressions As New List(Of Expression)()
        Do
            Dim exprPos = tokenVal.pos
            Dim expr = ParseExpression()
            Dim propName As String
            If TokenIdentifierIs("as") Then
                NextToken()
                propName = GetIdentifier()
                NextToken()
            Else
   开发者_高级运维             Dim [me] As MemberExpression = TryCast(expr, MemberExpression)
                If [me] Is Nothing Then Throw ParseError(exprPos, Res.MissingAsClause)
                propName = [me].Member.Name
            End If
            expressions.Add(expr)
            properties.Add(New DynamicProperty(propName, expr.Type))
            If tokenVal.id <> TokenId.Comma Then Exit Do
            NextToken()
        Loop
        ValidateToken(TokenId.CloseParen, Res.CloseParenOrCommaExpected)
        NextToken()
        'CODE added to support strongly-typed returns 
        Dim type As Type = If(newResultType, DynamicExpression.CreateClass(properties))
        Dim bindings(properties.Count - 1) As MemberBinding
        For i As Integer = 0 To bindings.Length - 1
            **bindings(i) = Expression.Bind(type.GetProperty(properties(i).Name), expressions(i))**
        Next
        Return Expression.MemberInit(Expression.[New](type), bindings)
    End Function
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜