1

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.

1
  • Actually, I'll bet it returns nothing if no member of list1 matches the first member of list2. 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. Commented Nov 19, 2014 at 5:40

3 Answers 3

1

Try this

var result= from x1 in list1
            join x2 in list2 on x1.FeatureID  equals x2.FeatureID 
            select x1;
Sign up to request clarification or add additional context in comments.

Comments

0

Thanks a lot guys!!! I got it using Expressions :)

var result = list1.Join(list2, t => t.FeatureID, t => t.FeatureID, (x, y) => new
        {
            ID = x.FeatureID,
            Name = x.FeatureName
        }).ToList();
result.ForEach(t => Console.WriteLine(t.Name));

Comments

0

You are basically looking for Left Join in LINQ:-

var result1 = list1.GroupJoin(list2,
                              l1 => l1.FeatureID,
                              l2 => l2.FeatureID,
                              (x, y) => new { x, y })
                                        .SelectMany(z => z.y.DefaultIfEmpty(),
                                        (a, b) => new
                                        {
                                             FeatureName = b == null ? "Feature don't match" : b.FeatureName
                                        });

Lamda expression for left join is a bit complex, you can use query syntax as well:-

var result = from l1 in list1
                      join l2 in list2
                      on l1.FeatureID equals l2.FeatureID into allFeatures
                      from finalList in allFeatures.DefaultIfEmpty()
                      select new
                        {
                           FeatureName = finalList == null ? "Feature don't match" : finalList.FeatureName
                        };

This is giving me this output:-

Feature1
Feature don't match
Feature don't match
Feature don't match
Feature5

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.