【问题标题】:LINQ to DATASET with Group Join and Group By (vb.net)LINQ to DATASET 与 Group Join 和 Group By (vb.net)
【发布时间】:2022-01-21 02:00:59
【问题描述】:

我有 2 个数据表,正在尝试使用左外连接来汇总其中的数据。加入此代码可以正常工作

Dim Journal = From entries In dt.AsEnumerable()
              Join inccodes In dtGL.AsEnumerable()
              On entries.Field(Of String)("GLCode") Equals inccodes.Field(Of String)("GLCode")                                                                                                                
              Group By keys = New With {Key .IncomeCode = entries.Field(Of String)("GLCode"), Key .IncomeDesc = .inccodes.Field(Of String)("GLCodeDesc")}
              Into ChargeSum = Group, sm = Sum(entries.Field(Of Decimal)("Amount"))
              Where sm <> 0
              Select New GL_Journal With {.IncomeCode = keys.IncomeCode, .IncomeDesc = keys.IncomeDesc, .LineAmount = sm}

                                                      `

但是,由于我真的想要一个左外连接,我想使用组连接而不是连接。 一旦我将加入更改为组加入组中的代码,在“.inccodes.field(Of String)(“GLCodeDesc”)”处的“.inccodes”突出显示错误“'inccodes'不是'匿名类型'" 我已经查看了很多关于 Group By 和 Group Join 的文档,但是关于它们的信息很少。 有任何想法吗?方法语法我会有更多选择/成功吗?

【问题讨论】:

    标签: vb.net linq join


    【解决方案1】:

    如果我尝试使用左外连接重现您的查询,我将执行以下操作:

    Dim dt As New DataTable
    dt.Columns.Add("GLCode", GetType(String))
    dt.Columns.Add("Amount", GetType(Decimal))
    dt.Rows.Add("111", 3251.21)
    dt.Rows.Add("222", 125.79)
    dt.Rows.Add("999", 10000)
    
    Dim dtGL As New DataTable
    dtGL.Columns.Add("GLCode", GetType(String))
    dtGL.Columns.Add("GLCodeDesc", GetType(String))
    dtGL.Rows.Add("111", "a")
    dtGL.Rows.Add("222", "b")
    dtGL.Rows.Add("333", "c")
    
    Dim Journal = From entries In dt.AsEnumerable()
                  Group Join inccodes In dtGL.AsEnumerable()
                    On entries.Field(Of String)("GLCode") Equals inccodes.Field(Of String)("GLCode")
                    Into Group
                  From lj In Group.DefaultIfEmpty()              
                  Group By keys = New With {Key .IncomeCode = entries.Field(Of String)("GLCode"), Key .IncomeDesc = lj?.Field(Of String)("GLCodeDesc")}
                    Into ChargeSum = Group, sm = Sum(entries.Field(Of Decimal)("Amount"))                
                  Select New With {.IncomeCode = keys.IncomeCode, .IncomeDesc = keys.IncomeDesc, .LineAmount = sm}
    

    【讨论】:

    • 不确定我将如何指示如何对我的聚合字段 Amount 求和。
    • 希望我更新的答案会更有用。
    • 太棒了!无错误。我将对其进行测试并验证结果。非常感谢!
    猜你喜欢
    • 2012-10-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多