Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Linq group by element and a sub element

Structure is basically like this:

Master:

  Id 
  DocNo
  Laws[]

Laws is as follows:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

LawNo
Id

I want to group by DocNo and LawNo
expected dictionary keys is:

{DocNo, LawNo}

And the value is

Master

I tried this but key is not like the expected

master.GroupBy(x => new {x.DocNo, _LawNo = x.Laws.Select(y => y.LawNo).Distinct()});

Thanks

>Solution :

If I understand correctly, you can try to use SelectMany before using GroupBy, SelectMany let LawNo flatten as DocNo

master.SelectMany(x=>x.Laws,(x,y)=>new{x.DocNo,y.LawNo})
      .GroupBy(x=> new {x.DocNo,x.LawNo});
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading