Question

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

Was it helpful?

Solution 2

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.

OTHER TIPS

You'll want something like this:

var results =
  from x in X
  from y in Y
  where x == y || X.All(item => !Y.Contain(item))
  select x;

The basic idea is: include the intersection if any intersection exists OR all items if no intersection exists.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top