Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to intersect multiple IEnumerable?

I don't understand why my variable selected doesn't contain the content of all the TempX variable. For example, in my case, the variable TempX containt one SuperObject but as soon as I reach the first intersect, it's lost and my View always show an empty list...

By the way, the blablabla.ToList() are real and complicated linq query. I put this to make it clearer.

Thanks and here is the code:

 public ActionResult Search(string q)
        {
            ViewBag.q = q;

            String[] strQueries = q.Split(' ');

            IEnumerable<SuperObject> selected = new List<SuperObject>();

            foreach (string str in strQueries)
            {  
                //Query 1
                IEnumerable<SuperObject> Temp1 = blablabla.ToList();

                //Query 2
                IEnumerable<SuperObject> Temp2 = blablabla2.ToList();

                //Query 3
                IEnumerable<SuperObject> Temp3 = blablabla3.ToList();

                //Query 4
                IEnumerable<SuperObject> Temp4 = blablabla4.ToList();

                selected = selected.Intersect(Temp1);
                selected = selected.Intersect(Temp2);
                selected = selected.Intersect(Temp3);
                selected = selected.Intersect(Temp4);
            }

            return View("Search", selected);
        }
like image 952
Jean-François Côté Avatar asked Oct 18 '25 14:10

Jean-François Côté


1 Answers

You probably want to use Union instead of Intersect. Here's the difference, I think it's self explanatory: Union, intersection, difference comparison.

like image 97
Dzienny Avatar answered Oct 20 '25 02:10

Dzienny