在VB.net中使用Linq查询对多个列进行分组

在VB.net中使用Linq查询对多个列进行分组,vb.net,linq,Vb.net,Linq,好的,我有下表 Dt.Columns.Add("A1") Dt.Columns.Add("A2") Dt.Columns.Add("A3") Dt.Columns.Add("A4") Dt.Rows.Add("A", "1", "11", "111") Dt.Rows.Add("B", "1", "11", "111") Dt.Rows.Add("C", "1", "12", "121") Dt.Rows.Add("D", "1", "12") Dt.Rows.Add("E", "2", "21

好的,我有下表

Dt.Columns.Add("A1")
Dt.Columns.Add("A2")
Dt.Columns.Add("A3")
Dt.Columns.Add("A4")
Dt.Rows.Add("A", "1", "11", "111")
Dt.Rows.Add("B", "1", "11", "111")
Dt.Rows.Add("C", "1", "12", "121")
Dt.Rows.Add("D", "1", "12")
Dt.Rows.Add("E", "2", "21", "211")
Dt.Rows.Add("F", "2", "21")
Dt.Rows.Add("G", "3")
Dt.Rows.Add("H", "3")
Dt.Rows.Add("I", "3", "31")
我有以下Linq查询

    Dim Query = From Row In Dt.AsEnumerable()
                Group Key = Row(1), Key2 = Row(2), Key3 = Row(3)
                By Key = Row(1)
                Into Test = Group
这给了我以下的结果

    "1", "11", "111"
    "1", "11", "111"
    "1", "12", "121"
    "1", "12"
但是我想让查询给我这个

"1", "11", "111"
"1", "12", "121"
"1", "12", null
"2", "21", "211"
"2", "21", null
"3", null, null
"3", "31", null
简单地说,我希望查询按最后3列进行分组,并输出这些列的分组。在Sql中,它类似于:

Select A2,A3,A4
From Dt
Group by A2,A3,A4
我已经和Linq搏斗了几个小时,但没能得到我想要的东西。

试试看

Dim Query = From Row In Dt.Rows
            Group Key = Row(1), Key2 = Row(2), Key3 = Row(3)
            By Key = Row(1), Key2 = Row(2)
            Into Test = Group
更新

考虑一下,你可能需要

By Key = Row(1), Key2 = Row(2), Key3 = Row(3)

是的,我想这就是诀窍,现在我需要将它转换成一个数据表,将进行更新。是的,这非常有效,顺便说一句,你能解释一下吗?我知道,在YYY中,每个XXX的工作都有点像,但从我的角度来看,其余的还是很神奇的。不幸的是,我不能,我只是拿了你的代码玩了一下。起初,我在Dt.AsEnumerable处得到一个错误,所以改为Dt.Rows。接下来,我想我们需要按多个列进行排序,按Key=Row1,Row2添加,得到了一个错误,Key=Row1,Key2=Row2尝试了这个错误。一旦我得到了工作,我看了看结果,发现我们需要添加第三个关键点。就像你说的魔法: