Alex Rivera | Logout

Find the intersection of two lists in linq?

Asked 2011-02-21T11:53:21.037
64

I have list of int A,B. i like to do the following step in linq

list<int> c = new List<int>();

for (int i = 0; i < a.count; i++)
{
    for (int j = 0; j < b.count; j++)
    {
        if (a[i] == b[j])
        {
            c.add(a[i]);
        }
    }
}

if its a and b is object , I need check particular properties like this manner and add list if it equals how can i do this in linq?

Edit
Report

1 Answer

93

You could use the Intersect method:

var c = a.Intersect(b);

This return all values both in a and b. However, position of the item in the list isn't taken into account.

answered 2011-02-21T11:56:37.170

Your Answer