Linq Select: Using a value from a list if available

Posted by musefan on Stack Overflow See other posts from Stack Overflow or by musefan
Published on 2012-11-29T10:53:33Z Indexed on 2012/11/29 11:04 UTC
Read the original article Hit count: 214

Filed under:
|

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)

© Stack Overflow or respective owner

Related posts about c#

Related posts about LINQ