How do I use SELECT GROUP BY in DataTable.Select(Expression)?

后端 未结 4 1837
刺人心
刺人心 2020-12-01 09:42

I try to remove the duplicate rows by select a first row from every group. For Example

PK     Col1     Col2
1        A        B
2        A        B
3                 


        
4条回答
  •  独厮守ぢ
    2020-12-01 09:52

    This solution sort by Col1 and group by Col2. Then extract value of Col2 and display it in a mbox.

    var grouped = from DataRow dr in dt.Rows orderby dr["Col1"] group dr by dr["Col2"];
    string x = "";
    foreach (var k in grouped) x += (string)(k.ElementAt(0)["Col2"]) + Environment.NewLine;
    MessageBox.Show(x);
    

提交回复
热议问题