Let's say I have a class called Features
class Features
{
public Features()
{
}
public Features(string id, string name)
{
this.FeatureID = id; this.FeatureName = name;
}
private string featureid;
private string featurename;
public string FeatureID
{
get { return featureid; }
set { featureid = value; }
}
public string FeatureName
{
get { return featurename; }
set { featurename = value; }
}
}
Then I created 2 Lists of the same type as following:
List<Features> list1 = new List<Features>();
list1.Add(new Features("1111111111", "Feature 1"));
list1.Add(new Features("2222222222", "Feature 2"));
list1.Add(new Features("3333333333", "Feature 3"));
list1.Add(new Features("4444444444", "Feature 4"));
list1.Add(new Features("5555555555", "Feature 5"));
List<Features> list2 = new List<Features>();
list2.Add(new Features("1111111111", "Feature 1"));
list2.Add(new Features("0002222222", "Feature 2"));
list2.Add(new Features("0003333333", "Feature 3"));
list2.Add(new Features("0004444444", "Feature 4"));
list2.Add(new Features("5555555555", "Feature 5"));
Then I compared these 2 lists using lambda expressions as following:
var newList = list1
.Select(
x => (new Features
{
FeatureID = x.FeatureID,
FeatureName = x.FeatureName
}
)
).Where(t=> t.FeatureID == list2.FirstOrDefault().FeatureID ).ToList();
newList.ForEach(t => Console.WriteLine(t.FeatureName));
So far this code returns only the first features ID that match in both lists...
The Question is:
How Can I loop on both lists using Lambda expression? I've tried Any and All, but nothing works but FirstOrDefault() as shown above..
thanks a lot, appreciated.
list1matches the first member oflist2. But it's unclear what output you are actually trying to achieve here. Please edit your question so that it states clearly the output you want, and why that's the correct output given the input.