c# - Find all intersecting data, not just the unique values -


I thought that I understood the difference , but it turns out that I was wrong.

list & lt; Int & gt; List1 = New list & lt; Int & gt; () {1, 2, 3, 2, 3}; & Lt; Integer & gt; List2 = New list & lt; Int & gt; () {2, 3, 4, 3, 4}; List 1 Intrigue (list2) = & gt; 2,3 // But what do I need: // = & gt; 2,3,2,3,2,3,3

I can understand in a way:

  var intersected = list1. Intercaster (list2 ); Var list3 = New list & lt; Int & gt; (); List3.AddRange (list1.Where (I => intersected.Contains (I))); List3.AddRange (list2.Where (I => intersected.Contains (I)));  

Is LINQ an easy way to achieve this?

I need to tell that I do not care which order in which the results are given.

2,2, 2, 3, 3, 3 will also be perfectly alright.

The problem is that I am using it on a large collection, so I need efficiency.

We are talking about items, not ints intits were just for easy examples, but I know that it will make a difference. Let's see if you want me to be wrong if I am wrong.

. You want: All elements of list 1, in order, which are shown in list 2, in order, according to all the elements of List 2, which also appears in the list 1. Yes?

seems straightforward. / P>

  return list 1.Where (x => list2.Contains (x)) .cockets (list 2. where (y = & gt; list 1. resource (y)) Olist ();  

Note that for large listings it is not not efficient if the lists contain every one thousand items, then it compares two million. If you are in that situation, you want to use a more efficient data structure for membership testing:

  list1set = new HashSet (list1); List2set = new hashset (list2); Return List 1.Where (x => list2set.Contains (x)) .ConCat (list2.Where (y = & gt; list1set.Contains (y)).) List ();  

Which only compares two thousand, but uses potentially more memory.


Comments

Popular posts from this blog

sql - dynamically varied number of conditions in the 'where' statement using LINQ -

asp.net mvc - Dynamically Generated Ajax.BeginForm -

Debug on symbian -