简体   繁体   中英

linq Group By from DataTable

I have DataTable Like this

Thank you Bob Vale for your help

what is (Select(X,i) mean in your linq, but as i made a mistake in my table I have this

   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

I want to have some thing like that

  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  |        |   

How can I make it? I can make Group By

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 

no= g.Key.no };

but it that could't give the result

thank you for your attention

How about this:

// 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 
                                                }));

您可以使用linq查询作为var结果= cableDataTable.AsEnumerable()。GroupBy(g => new {g.FB,g.FP})。Select(x => x);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM