4

(See my code snippet below) I want to find all items of coll1 that matched to items of coll2 (number of items of coll2 <= number of items of coll1) and put the query result in coll3. How to achieve it using linq and lambda expression? Surely, I can simply copy coll2 to coll3 :-) but that is not my goal. I want to know the ways using linq and lambda to replace such conventional logic construct. Thank you in advance.

        var coll1 = new List<int>() { 1, 2, 3, 4, 5 };
        var coll2 = new List<int>() { 2, 4 };
        var coll3 = new List<int>();
        foreach ( var selected in coll2 )
        {
            foreach ( var item in coll1 )
            {
                if ( selected == item )
                {
                    coll3.Add(item);
                }
            }
        }

6 Answers 6

14

You can use Intersect

coll1.Intersect(coll2);

But this wont work as expected(see King King's comment)..You can instead do this

coll2.Where(x=>coll1.Any(y=>x==y));
Sign up to request clarification or add additional context in comments.

4 Comments

This seems the best solution to me. +1
this won't work as expected: if col2 = {1,2,3,4}, col1 = {2,2,4}. This will return {2,4} while the result should be {2,2,4}?
@KingKing nice observation king..thanks to point it out..corrected the ans..:)
@Anirudh: it works for me like this: coll3 = coll2.Where(x => coll1.Any(y => x == y)).ToList(); .. +1 still.
6
coll3 = coll1.Where(i => coll2.Contains(i)).ToList();

Update. A little bit simpler, as suggested in comments:

coll3 = coll1.Where(coll2.Contains).ToList();

1 Comment

You can simplify that to: var coll3 = coll1.Where(coll2.Contains).ToList();
2

As a first step, you can use a where clause to make your code more readable :

    var coll1 = new List<int>() { 1, 2, 3, 4, 5 };
    var coll2 = new List<int>() { 2, 4 };
    var coll3 = new List<int>();
    foreach (var selected in coll2)
    {
        coll3.AddRange(coll1.Where(item => selected == item));
    }

Comments

2

Use Intersect: http://msdn.microsoft.com/en-us/library/bb460136.aspx

var coll3 = coll1.Intersect(coll2)

1 Comment

it doesn't work as expected (see comment of King King to Anirudh's answer).
1

You could do this; not sure if it's more readable tho!

var coll3 = (from selected in coll2
                from item in coll1
                where selected == item
                select item).ToList();

3 Comments

This exactly what resharper proposes too.
I wonder about the downvote. This code is correct, so why? Although it looks like some numpty has just downvoted all the answers here...
@Matthew Watson: who downvoted? strange. I am sure I give u +1 :-)
0

If you do not want to directly assign the result to your list, you could add 'ForEach' to your Linq statement:

coll1.Where(i => coll2.Contains(i)).ToList().ForEach(i => coll3.Add(i));

You might want to check for Distinct though

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.