Ab Laxman

Ab Laxman

  • NA
  • 14
  • 1.1k

Multi-parameter anonymous types?

Mar 1 2018 11:48 PM
I recenty stumbled upon a piece of LINQ code written below
  1. var cons = xdoc.Descendants("xref")  
  2.         .Where(x=>x.Attribute("rid").Value.Contains("ref"))  
  3.         .GroupBy(x=>x.Parent)  
  4.         .Select(grp=> new  
  5.                 {  
  6.                     Parent = grp.Key,  
  7.                     ConsecutiveNodes = grp.Select((n, i)=> new  
  8.                                                   {  
  9.                                                     Index = i+1,  
  10.                                                     Node = n  
  11.                                                   }),  
  12.                     Count = grp.Count()  
  13.                 })  
  14.         .ToList(); 
 Now I cannot understand the part
  1. .Select(grp=> new    
  2.                 {    
  3.                     Parent = grp.Key,    
  4.                     ConsecutiveNodes = grp.Select((n, i)=> new    
  5.                                                   {    
  6.                                                     Index = i+1,    
  7.                                                     Node = n    
  8.                                                   }),    
  9.                     Count = grp.Count()    
  10.                 })  
 Can anyone break it down for me because I cannot figure it out on my own, there are a lot of new things that I haven't seen before...like an anonymous type inside and anonymous type? and what is the grp.Select((n, i) thing? how does it work?
 

Answers (1)