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
another with items to exclude
and finally disjoin them.
Caveat: Entities in the disjoint query must be of the same type or must have an IEqualityComparer interface defined for the Except function to execute without run-time errors.
You might want to consider using the Concat or Union LINQ functions, in case your business logic calls for combining objects into a single list.
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 interface defined for the Except function to execute without run-time errors.
You might want to consider using the Concat or Union LINQ functions, in case your business logic calls for combining objects into a single list.
Comments