If you use a collection in memory as a filter, it is best to use the negation of Contains (). Please note that this may fail if the list is too long, in which case you will need to choose a different strategy (see below to use the strategy for a fully database-oriented query).
var exceptionList = new List<string> { "exception1", "exception2" }; var query = myEntities.MyEntity .Select(e => e.Name) .Where(e => !exceptionList.Contains(e.Name));
If you exclude another database query using Except , this might be a better choice. (Below is a link to the supported Set extensions in LINQ to Entities)
var exceptionList = myEntities.MyOtherEntity .Select(e => e.Name); var query = myEntities.MyEntity .Select(e => e.Name) .Except(exceptionList);
This assumes a complex object in which you exclude certain of them, depending on some property of another table, and want the names of the objects not to be excluded. If you need the whole object, then you will need to create exceptions as instances of the entity class so that they satisfy the default equality operator (see docs ).
tvanfosson Jan 11 '09 at 14:48 2009-01-11 14:48
source share