Skip to main content

Posts

Showing posts from June, 2013

Disjoint queries in LINQ to SQL

I find LINQ to SQL expressions very useful when running queries against Entity Framework objects; however, running disjoint queries in LINQ (queries that require filtering one list and running the results against another) is not as simple using inner joins or correlated sub-queries [i.e.  in SQL "where [someID] not in ([some query])']. To run a disjoint query in LINQ to SQL you would have to write three separate queries. One with all data you need Code Snippet var myDataUniverse = from ent in [SomeEntity]                       select ent; another with items to exclude Code Snippet var myExcludedList = from ent in  [SomeEntity]                        // where some logic to filter excluded entities                       select ent; and finally disjoin them. Code Snippet var myFinalList = myDataUniverse.Except(myExcludedList); Caveat : Entities in the disjoint query must be of the same type or must have an IEqualityComparer  inte