LINQ - 基于多个不同的列返回包含表的所有列的行

时间:2013-05-09 22:44:51

标签: c# linq

我有一张桌子,

**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基于Columns - Matter No和Client No.,即我需要看到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中实现这一目标?

提前致谢

2 个答案:

答案 0 :(得分: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();

答案 1 :(得分:0)

试试这个:

  xmlhttp.onreadystatechange = function() {
    if (xmlhttp.readyState == XMLHttpRequest.DONE ) {
       if (xmlhttp.status == 200) 
       {
            $("#table1").find("tr:gt(0)").remove();
            var table1 = $("#table1");
            var Data = JSON.parse(xmlhttp.responseText);
            $.each(Data, function(key,value) { 
                var rowNew = $("<tr><td></td><td></td><td></td><td></td></tr>");
                rowNew.children().eq(0).text(value['idp']); 
                rowNew.children().eq(1).text(value['producator']); 
                rowNew.children().eq(2).text(value['culoare']); 
                rowNew.children().eq(3).text(value['pret']); 
                rowNew.appendTo(table1); 
            });
            createBox();
       }
       else if (xmlhttp.status == 400) {
          alert('There was an error 400');
       }
       else {
           alert('something else other than 200 was returned');
       }
    }
};
相关问题