简体   繁体   中英

Linq Sub Grouping Query

I'm trying to go from the First Block to the 2nd Block via Linq Query, is that possible?

Year  Quarter WordDoc   SpreadSheet
---------------------------------------------------
2005  Q1      q1file.doc  -
2005  Q1      -                   q1file.xls
2005  Q2      q2file.doc  -
2005  Q2      -                       q2File.xls
2005  Q3      q3file.doc  -
2005  Q3      -                       q3File.xls
2006  Q4      q4file.doc  -
2006  Q4      -                       q4File.xls

2nd block:

Year  Quarter WordDoc    SpreadSheet
---------------------------------------------------
2005
      Q1      q1file.doc q1File.xls
      Q2      q2file.doc q2File.xls
      Q3      q3file.doc q3File.xls
2006          
      Q4      q4file.doc q4File.xls

Unfortunately all I can figure out so far is grouping by Year:

var results = 
    from entry in SampleDeals
    group entry by entry.Year into years
    select years;

results.Dump();

Assuming all the groups have 2 elements, one with a word document the other with a spreadsheet:

var groups = SampleDeals.GroupBy(deal => new {Year = deal.Year, Quarter = deal.Quarter});
var whatYouWant = groups.Select(group => new {Year = group.Year, Quarter = group.Quarter,       
                         WordDoc = group.First().WordDoc ??  group.ElementAt(1).WordDoc, 
                         SpreadSheet = group.First().SpreadSheet ??  group.ElementAt(1).SpreadSheet});

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