Search code examples
sqldatabaselinqsetset-theory

How to get the Intersection of Multiple sets but Ignore Sets that Don't Intersect


Hi I am looking to get elements from a table where the elements I'm interested are dependent on a number of variables that the user selects, these include radio buttons drop down lists and select lists. Each variable will decide on which Ids i want from a table.

I need to be able to ignore variables that don't intersect or are empty.

Here's an example

Set A = {1,2,3,4,5}
Set B = {2,5,6,7,8}
Set C = {Cat, Dog}

A intersection B intersection C == {}

but I need the answer {2, 5}

Is there a way of getting this from these sets without using conditional statements? I have a lot of variables and I would like to do this in one statement if possible.

Thanks


Solution

  • I got this working by getting the distinct elements of each table that I needed as a List.

    Next I created a Dictionary List of type

    I went through each list individually every time I found a new object I added it to the dictionary. If the object already existed I increased the count value for that object.

    After that I found the highest value int Max in the dictionary.

    Then I selected the objects from the dictionary where the count matched Max.

    I would have liked to do something cleaner but hey it works. Which is fine for me I'm only developing a prototype.