I am going to use some basic stripped down examples to illustrate my problem.
I have a class:
class Item
{
int ID;
bool Selected;
}
Now lets say I have two lists of the Item class:
List<Item> ListA = GetListA();
List<Item> ListB = GetListB();
Now I want to create a third list that has all the items from ListB. The important thing is that if a match (same ID) if found in ListA then I want to use that Selected value, otherwise I want to keep the Selected value of the item that is in ListB.
I am creating the third list as follows:
List<Item> ListC = from item in ListB
select new Item
{
ID = item.ID,
Selected = item.Selected// <-- should use value form ListA if available
};
Important: I don’t want to seem ignorant, but I do not want to change the way ListC is created. By that I mean I want to use the “linq select” method, and I want to use a “one liner” that assigns the Selected value… I know there are other ways to create the list which will work just fine, but then I won’t learn anything new.
I have tried a couple of things so far…
I know this will work, but I don’t want to query ListA twice:
Selected = ListA.Any(x => x.ID == item.ID) ? ListA.First(x => x.ID == item.ID).Selected : item.Selected
and I also tried using DeafultIfEmpty but I don’t think that is the right think for this situation… because it didn’t work, and it seems it is more use if ListA was empty (which I don’t care about)
You can modify your last line of code as follows:
For
DefaultIfEmpty, you’d have to dowhich is harder to follow but does essentially the same thing.
As Dominic has noted below, if you use
SingleOrDefault/Singlein place ofFirstOrDefault/First, you will get an exception if you find two items inListAwith the sameID– which may be a check you want to introduce.(There are better/more efficient ways of doing this, but as you say, you want to fix this way of doing it, not use a totally different way.)