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 get new list from a list of object property which is a list itself

So I have a list like this:

{
  "images": [
    {
      "imageLink": "link1",
       "pIds": [
        "id1"
      ]
    },
    {
      "imageLink": "link2",
      "pIds": [
        "id2"
      ]
    },
    {
      "imageLink": "link3",
      "pIds": [
        "id2"
      ]
    }
  ]
}

I want to create a list which contains all the unique pIds from above list . How Do I do so?
I am trying it like this

var pIds  = oldList.Select(p=>p.Ids).ToList();

but I dont want List of lists just the elements inside the pIds in oldList.

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

like:

pIds = ["id1","id2"]

>Solution :

You are looking for SelectMany to flatten the nested enumerations:

var pIds  = oldList
  .SelectMany(p => p.Ids)
  .Distinct() // to get unique ids
  .ToList();
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