1

我有这样的数据表

感谢 Bob Vale 的帮助

(Select(X,i) 在您的 linq 中是什么意思,但是由于我在表中犯了一个错误,所以我有这个

   No |  Size   | Type   |   FB   |   FP
----------------------------------------
 100  |   2     | typeA  |   FB1  |    A1        
 101  |   3     | typeB  |   FB1  |    A1     
 101  |   4     | typec  |   FB1  |    A1         
 103  |   4     | typeC  |   FB2  |    A2         
 103  |   5     | typeD  |   FB2  |    A2         
 103  |   6     | typeE  |   FB2  |    A2

我想要这样的东西

  No |  Size   | Type   |   FB   |   FP    
    ---------------------------------    
100  |  2     | typeA  |   FB1  |    A1    
101  |  3     | typeB  |   FB1  |    A1     
     |  4     | typec  |        |        
103  |  4     | typeC  |   FB2  |    A2         
     |   5    | typeD  |        |        
     |   6    | typeE  |        |   

我怎样才能做到?我可以分组

var result = from row in cableDataTable.AsEnumerable()
             group row by new 
             {
                 FB = row.Field<string>("FB"),
                 FP = row.Field<string>("FP"), 
                 Size = row.Field<int>("Size"),
                 Type = row.Field<int>("Type"), 
                 no= row.Field<int>("no"),
             } into g
             select new
             {
                 FB = g.Key.FB,
                 FP = g.Key.FP,
                 Size = g.Key.Size,
                 Type = g.Key.Type 

没有= g.Key.no };

但它不能给出结果

感谢您的关注

4

2 回答 2

3

这个怎么样:

// First declare a conversion from the DataTable to an anon type
var rows = cableDataTable.AsEnumerable()
                         .Select(x => new { 
                                           Size = x.Field<int>("Size"),
                                           Type= x.Field<string>("Type"),
                                           FB = x.Field<string>("FB"),
                                           FP = x.Field<string>("FP")
                                          });

// Now use group by, ordering and select many to select the rows
var result =  rows.GroupBy (row => new {row.FB, row.FP} )
                  .OrderBy (g => g.Key.FB)
                  .ThenBy(g => g.Key.FP)
                  .SelectMany(g => g.OrderBy(row => row.Size)
                        .Select((x,i) =>
                                               new { 
                                                 Size = x.Size,
                                                 Type = x.Type,
                                                 FB = (i==0) ? x.FB : null,
                                                 FP= (i==0) ? x.FP : null 
                                                }));
于 2013-09-18T10:15:30.543 回答
0

您可以将 linq 查询用作 var result = cableDataTable.AsEnumerable().GroupBy(g => new { g.FB, g.FP}).Select(x => x);

于 2013-09-18T10:19:26.457 回答