访问通过动态链接查询返回的匿名类型

发布于 2024-12-05 11:54:25 字数 2363 浏览 0 评论 0原文

我一直在尝试以匿名类型的形式访问从动态 linq 查询返回的数据。我发现的建议建议我应该创建一个自定义类型,并在 Select 子句中使用 new 关键字使用它。我被引导至以下问题以获取代码示例:

System.LINQ.Dynamic: Select(" new (...)") into a List; (或的任何其他可枚举集合)

这确实是一个很好的例子,我将其合并到我的代码中(这是VB,所以我必须做一些翻译)。

我的代码编译正常,但是当我运行它时,我收到以下错误:

“值不能为空。参数名称:成员”,示例中的以下行: 绑定[i] = Expression.Bind(type.GetProperty(properties[i].Name), 表达式[i]);

这似乎链接到表达式(i),它正确包含两个项目,因为我从数据库表返回两个字段。 properties(i) 正确保存这两个字段的名称。关于会员的价值应该是什么以及应该在哪里找到它有什么想法吗?是数据库里的东西吗?

该查询的 where 子句有效,当我作为匿名类型运行它时,它会返回记录(或者更确切地说是记录中的两个字段)。返回的字段包含不为空的数据。

这是我之前问题中提供的示例代码的 VB 版本。我已将发生错误的行加粗或 **。关于造成这种情况的原因有什么想法吗?

非常感谢。

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文