good afternoon everybody
the question is kinda simple but I've been having problems the whole afternoon
i have 2 lists:
and i want to compare them but i want to obtain the id that doesn't have a pair (if it exists)
i was wondering if there's a c# or linq method to identify the values that are different in two arrays
example
if i have
List<int> ids = {1,2,3,4,5}
and
List<objectX> x = (contains id,code, and description)
and i was trying something like
foreach (int id in ids)
{
foreach (objectX item in x)
{
if (item.id == id)
{
break;
}
else
idDiferentes.Add(id);
}
}
but like you can imagine it doesn't work
for example
ids= {1,2,3,4}
objectx[id] ={1,3,2}
the ids are different when i compare them so i get a bigger list that the one i need
i also tried with an linq outer join but i don't understand how it works pretty well
var idsWithoutObjects = ids.Except(x.Select(item => item.id));
What you are after is the Except extension method. It gives you the set difference between two sequences.
So you can do something like this (pseudo c#-code):
var idDifferences = x.Select(item => item.id).Except(ids);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With