Intersection of two data sets (lists)

I have two data sets (lists of complex objects or SQL data - LINQ to Entities) where im trying to find the intersection of two data sets. In particular, the intersection of the Complex property, "HashData", as shown below:

SQL data

The set on the left is likely to be around 10,000 lines, while the set on the right is always a subset of about 100 lines. I understand that if I sort the set of β€œHashdata” on the left while storing it, the search will be much faster using some kind of binary search algorithm, however I cannot do this for reasons not related to the question.

A smaller subset of data is never stored in SQL (shown only in the SQL table below for clarification). It is presented in List<ShowData> at runtime.

At the moment, I am doing a miserable loop through the data and matching them (where recording is 100 lines of the list and ShowData is the list of lines 10,000):

 List<ShowData> ShowData = (from showData in context.ShowDatas where (showData.Show.Id == advert.Id) orderby showData.HashData ascending select showData).ToList(); foreach (ShowData recording in recordingPoints) { foreach (ShowData actual in ShowData) { if (recording.HashData == actual.HashData) { } } } 

So basically what I'm trying to do is:

Returns a list of ShowData objects (large set), where any HashData (from a small set) is in ShowData, but inside the original LINQ to Entity query to the database.

I got closer:

 private IEnumerable<ShowData> xyz(List<ShowData> aObj, List<ShowData> bObj) { IEnumerable<string> bStrs = bObj.Select(b => b.HashData).Distinct(); return aObj.Join(bStrs, a => a.HashData, b => b, (a, b) => a); } 
+6
source share
2 answers

Since you are using IEnumerable, you can use the Intersect extension method instead of Join. If you want to return a large set, you will need to cross the result of a large set of queries with a smaller set. You will need to write IEquality Comparer, as shown here: http://msdn.microsoft.com/en-us/library/bb355408.aspx to compare your objects, then call the Intersect extension method:

 return bStrs.Intersect(aObj, new MyEqualityComparer()); 
+6
source

Something like this might work (warning not verified):

 private IEnumerable<ShowData> xyz(List<ShowData> aObj, List<ShowData> bObj) { return aObj.Where(sd1 => bObj.Select(sd2 => sd2.HashData).Contains(sd1.HashData)); } 
+1
source

Source: https://habr.com/ru/post/904341/


All Articles