## Find the intersection of two lists in linq?

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?

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.

**Intersection of two lists using LINQ,** It does know how to find equality in an ID most times however. EDIT 2: The Intersect Any is the way to go because you are want to compare a� Produces the set intersection of two sequences by using the specified IEqualityComparer<T> to compare values. Intersect<TSource> (IEnumerable<TSource>, IEnumerable<TSource>) Produces the set intersection of two sequences by using the default equality comparer to compare values.

You can use Intersect:

var a = new List<int>(); var b = new List<int>(); var c = a.Intersect(b);

**Using linq to find the intersection of two lists but each list contains ,** Hello,. I have two different classes lets call them A and B. Both classes have a string property called Name. I have two lists ListA which contains� It does know how to find equality in an ID most times however. EDIT 2: The Intersect Any is the way to go because you are want to compare a list to a list. public List<User> GetUsers(User admin){ var adminCompanyIDs = admin.Companys.Select(c => c.ID); return Users.Where(user=>user.Companys.Select(c => c.ID).Intersect(adminCompanyIDs).Any()).ToList(); }

Produce a list `c`

containing all elements that are present in both lists `a`

and `b`

:

List<int> c = a.Intersect(b).ToList();

**Intersect two lists in C#,** List val1 = new List { 25, 30, 40, 60, 80, 95, 110 }; List val2 = new List { 27, Now, use the Intersect() method to get the intersection between two lists. Linq; class Demo { static void Main() { List<int> val1 = new List<int> { 25,� Concat of two list using LINQ. 1432. Why not inherit from List<T>? 3. C#- Select specific items from a list based on partial intersection with another list (Linq

The LINQ equivalent of your code is:

var c = from i in Enumerable.Range(0, a.Count) from j in Enumerable.Range(0, b.Count) where a[i] == b[j] select a[i]; var cList = c.ToList();

But it's much nicer to do:

var c = from aItem in a join bItem in b on aItem equals bItem select aItem; var cList = c.ToList();

But this doesn't filter duplicates. To filter duplicates *completely*, you can do:

var cList = a.Intersect(b).ToList();

If you want duplicates to show up as many times as they do in `b`

, for example:

var aSet = new HashSet<int>(a); var cList = b.Where(aSet.Contains) .ToList();

**LINQ Except, Intersect and Union Method/ Operator in C#,** Intersect returns the common elements of both entities and returns the result as a new entity. For example, there are two lists, the first list contains 1, 2 and 3 the and second list contains 3, 5 and 6. Then the intersect operator will return 3 as the result because 3 exists in both lists. How to find the set difference between two lists (LINQ) (C#) 07/20/2015; 2 minutes to read +6; In this article. This example shows how to use LINQ to compare two lists of strings and output those lines that are in names1.txt but not in names2.txt.

This is my version of intersection:

var a = new List<int>(); var b = new List<int>(); // intersection var c = a.Where(x => b.Any(y => x == y)).ToList();

Intersect returns the common elements of both entities and returns the result as a new entity. For example, there are two lists, the first list contains 1, 2 and 3 the and second list contains 3, 5 and 6. Then the intersect operator will return 3 as the result because 3 exists in both lists. Code Example

The change needed is to specify the generic type parameter of the Aggregate method on the last line like below: var intersection = listOfLists.Aggregate<IEnumerable<int>> ( (previousList, nextList) => previousList.Intersect(nextList) ).ToList(); share.

In this method we set () the larger list and then use the built-in function called interscetion () to compute the intersected list. intersection () is a first-class part of set.

LINQ Intersect operator is used to find common elements between two sequences (collections). Intersect opertor comes under Set operators category in LINQ Query operators. For example, we have two collections A = { 1, 2, 3 } and B = { 3, 4, 5 }.

##### Comments

- This code will return duplicates in c if there are duplicates in a or b. eg a = {1,2,2,3} and b = {2,2,4,6} will give c = {2,2,2,2}. Is this what you want or are your lists unique anyway so its not important? I ask just because the obvious linq answer will give c={2,2} as that is the intersection of the lists.
- @Femaref: really? Ah, ok. At least my point still stands. I'm still not great at linq stuff like this. :)
- That's not confined to LINQ. This might be a good read for you.
- The title is misleading. For anyone who found this page and actually want to compare two enumerables for equality Linq has a method called SequenceEqual.
- To save a google for others who come across this, the opposite LINQ method is 'Except'.
- OP's code doesn't take position into account either so that is not an issue - however OP's code does something quite unusual that
`Intersect`

does not: for each distinct element**e**that exists in both`a`

and`b`

,**e**is added to`c`

, where**n**times**n**= [the number of times**e**occurs in`a`

] * [the number of times**e**occurs in`b`

]. This is not an intersect at all, but maybe OP intended to make an intersect and just implemented it wrong? - Is it possible to set condition in the intersection?
- Yes, there's an override that takes a second parameter, which you can use to compare the lists based on specific properties rather than the default property comparison operator. msdn.microsoft.com/en-us/library/bb355408(v=vs.110).aspx
- Is that even going to be the same? That won't return the value in a twice if it is in b twice will it?
- Hmmm...not sure, and good point. I'll need to experiment with that to find out.