locked
How to join 2 IQuerables RRS feed

  • Question

  • User-222276809 posted

    I have 2 queries which have same table joins but different where conditions. I need to compare the result of these 2 queries. Instead of saying .ToList() on each query and getting the result into memory and comparing can I join these 2 IQuerables and then perform the comparison? My goal is to hit DB only once. Please help!!

    Thursday, April 19, 2018 9:22 PM

All replies

  • User283571144 posted

    Hi praveen_katta,

    According to your description, I suggest you could write a sql query to select the result which you want instead of using linq to get the result.

    I suggest you could refer to below query example:

    Select  * FROM 
    (
    QUERY A GOES HERE 
    ) AS A
    LEFT JOIN
    (
    QUERY B GOES HERE 
    ) AS B
    ON A.Col1=B.Col1 AND A.Col2=B.Col2 ....
    

    Best Regards,

    Brando

    Friday, April 20, 2018 2:37 AM
  • User1120430333 posted

    If you close the connection after you do the ToList(), then you have disconnected objects in the LIst<T>. As the objects are iterated over with a loop within the List<T> as an example, the objects are not populated again from the database, because the objects are disconnected. 

    Tuesday, April 24, 2018 4:50 AM