1

我有一张桌子,

**Matter No      Client No    Client Name  Invoice No  Invoice Date    Invoice Amt**

1111-0001          1111          ABC            101       01/01/2013       100.00 
1111-0001          1111          ABC            102       02/01/2013       200.00
1111-0001          1111          ABC            103       03/01/2013       300.00
1111-0001          1111          ABC            104       04/01/2013       400.00
1111-0001          1112          DEF            105       05/01/2013       500.00
1111-0001          1113          GHI            106       06/01/2013       600.00

基于上述情况,我希望返回所有列,但基于列 - 事项号和客户号的 DISTINCT ROWS 即我需要看到 3 行作为输出:

**Matter No      Client No    Client Name  Invoice No  Invoice Date    Invoice Amt**

1111-0001          1111          ABC            101       01/01/2013       100.00 
1111-0001          1112          DEF            105       05/01/2013       500.00
1111-0001          1113          GHI            106       06/01/2013       600.00

我如何在 LINQ 中实现这一点?

提前致谢

4

2 回答 2

5
from x in table
group x by new {x.MatterNo, x.ClientNo}
into mygroup
select mygroup.First();

或者,如果您更喜欢这种语法

list.DistinctBy(x => new {x.MatterNo, x.ClientNo});

如果您不处理列表,您可以尝试

from DataRow drow in dtable.Rows
group drow by new {MatterNo = drow["Matter No"], ClientNo = drow["Client No"]}
into myGroup
select myGroup.First();
于 2013-05-09T22:48:54.833 回答
0

尝试这个:

DataTable result = dt.AsEnumerable()
                     .GroupBy(x => x.Field<int>("COLUMN NAME"))
                     .Select(x => x.First()).CopyToDataTable();
于 2017-06-26T17:10:50.837 回答