Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to compare list efficiently?

I'm currently working on a web application in asp.net. In certain api-calls it is necessary to compare ListA with a ListB of Lists to determine if ListA has the same elements of any List in ListB. In other words: If ListA is included in ListB.

Both collections are queried with Linq of an EF-Code-First db. ListB has either one matching List or none, never more than one. In the worst case ListB has millions of elements, so the comparison needs to be scalable.

Instead of doing nested foreach loops, i'm looking for a pure linq query, which will let the db do the work. (before i consider multi column index)

To illustrate the structure:

//In reality Lists are queried of EF 
var ListA = new List<Element>();
var ListB = new List<List<Element>>(); 
List<Element> solution;
bool flag = false;
foreach (List e1 in ListB) {
   foreach(Element e2 in ListA) {
        if (e1.Any(e => e.id == e2.id)) flag = true;
        else {
             flag = false;
             break;
        }
    }
        if(flag) {
           solution = e1;
           break;
        }
}

Update Structure

Since its a EF Database i'll provide the relevant Object Structure. I'm not sure if i'm allowed to post real code, so this example is still generic.

//List B
class Result {
       ...
       public int Id;

       public virtual ICollection<Curve> curves; 

       ...
}

class Curve {
       ...
       public int Id;

       public virtual Result result;
       public int resultId;

       public virtual ICollection<Point> points;
       ...
}
public class Point{
    ...
    public int Id;
    ...
}

The controller (for the api-call) wants to serve the right Curve-Object. To identify the right Object, a filter (ListA) is provided (which is in fact a Curve Object) Now the filter (ListA) needs to be compared to the List of Curves in Result (ListB) The only way to compare the Curves is by comparing the Points both have. (So infact comparing Lists) Curves have around 1 - 50 Points. Result can have around 500.000.000 Curves

It's possible to compare by Object-Identity here, because all Objects (even the filter) is re-queried of the db.

I'm looking for a way to implement this mechanism, not how to get around this situation. (e.g. by using multi column index (altering the table))

(for illustration purposes):

class controller {
    ...
    public Response serveRequest(Curve filter) {
         foreach(Curve c in db.Result.curves) {
               if(compare(filter.points , c.points)) return c;

         }
    }
}
like image 592
Laurin Avatar asked Feb 23 '17 10:02

Laurin


People also ask

Can you compare lists with == in Python?

We can club the Python sort() method with the == operator to compare two lists. Python sort() method is used to sort the input lists with a purpose that if the two input lists are equal, then the elements would reside at the same index positions.

How do I compare two lists and differences in Python?

The difference between two lists (say list1 and list2) can be found using the following simple function. By Using the above function, the difference can be found using diff(temp2, temp1) or diff(temp1, temp2) . Both will give the result ['Four', 'Three'] .

How do you compare two lists of strings in Python?

Use sort() method and == operator to compare lists The sorted list and the == operator are used to compare the list, element by element.


1 Answers

Use Except:

    public static bool ContainsAllItems(IList<T> listA, IList<T> listB)
    {
        return !listB.Except(listA).Any();
    }

the above method will tell if listA contains all the elements of listB or not..and the complexity is much faster than O(n*m) approach.

like image 54
Sombir Kumar Avatar answered Oct 05 '22 19:10

Sombir Kumar