I have two list
List A
List<test> populate = new List<test>();
{
populate.Add(new test(){ID = 1, name="AAA", nameID=1, type=1, isSelected=false});
populate.Add(new test(){ID = 2, name="BBB", nameID=2, type=1, isSelected=false});
populate.Add(new test(){ID = 3, name="CCC", nameID=3, type=1, isSelected=false});
}
List B
List<build> populateBuild = new List<build>();
{
populateBuild.Add(new test(){ID = 1, name="AAA", nameID=1, type=1, isSelected=false});
populateBuild.Add(new test(){ID = 3, name="CCC", nameID=3, type=1, isSelected=false});
}
What I want to achieve is:
1) I want new list, (List C)
2) In List C, I want all the data in List A, but the value of isSelected in List A, will changed to TRUE, when it is compared to data in List B
3) Means, if List B is exist in list A, the value of isSelected in list A will changed to TRUE and added to list C
4) if List B is not exist in List A, it will still be added to List C, but without changing the isSelected value(remain false).
Thank you,
I assume you mean List<test> populateBuild = new List<test>();
(not List<build>
). You can generate the 3rd list using
// Get the ID's of the 2nd list
IEnumerable<int> populateBuildIds = populateBuild.Select(x => x.ID);
// Initialize the 3rd list
List<test> listC = new List<test>();
// Copy all elements from the first list and update the isSelected property
foreach (test t in populate)
{
listC.Add(new test()
{
ID = t.ID,
name = t.name,
nameID = t.nameID,
type = t.type,
isSelected = populateBuildIds.Contains(t.ID) // true if also in 2nd list
});
}