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

Create a one to many collection from linq result setC#

I have a database table which contains one to many data collection. From that table I need to make a dictionary which represents the data. the key will be the master Id and the values will be the ids associated to that master id. my dataset is like the following

and the expected dictionary should be like the following

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

enter image description here

I tried the following code but it didnt work as expected. Instead of Dictionary<int,List> it returned Dictionary<key,groupbydata collection>.

 Table.GroupBy(item => item.MasterId).ToDictionary(item => item.Key);

Any thoughts?

>Solution :

You need to cast the values inside the grouping to the dictionary

var test = Table.GroupBy(item => item.MasterId).ToDictionary(g => g.Key, v=> v.Select(x => x.InternalValue));

so you got a Dictionary<MasterID, IEnumerable >

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