Home > Software design >  Filter a list based on another list containing IEnumerable<Guid> using linq
Filter a list based on another list containing IEnumerable<Guid> using linq

Time:02-16

I am looking for a way to filter a list based on another list that contains only the ids. If List A does not contain any of the ids in listOfGuids, get the filtered List that does not contain the ids

IEnumerable<Guid> listOfGuids = some list of Guid.
var filteredList = ListA.All(x=>listOfGuids.Where(x=>x.id!={all the items in list of Guid}))

Eg:

listOfGuids = {
    9c105700-98af-4c23-92ac-fd2dc5664b7a,
    7d6e0918-fb05-4b37-80f4-c7e5ccaa094d
}

ListA =[
    {id="9c105700-98af-4c23-92ac-fd2dc5664b7a", name="abc"},
    {id="7d6e0918-fb05-4b37-80f4-c7e5ccaa094d", name="def"},
    {id="6fb0bcf3-9ca6-40c8-9fa1-f7557ef09558", name="ghi"}
]

Should return filteredList as

[{{id="6fb0bcf3-9ca6-40c8-9fa1-f7557ef09558", name = "ghi"}}]

CodePudding user response:

You want items Where it's not true that the list Contains the id:

var filteredList = ListA.Where(x => !listOfGuids.Contains(x.id))
  • Related