Reputation: 975
I have the following:
IQueryable<Entity1, Entity2> query = //some query with joins
return query.Distinct().ToList();
This works fine. What I want to do is to extend the query by adding some additional parts to it, like this:
IQueryable<Entity1, Entity2> query = //some query with joins
return query.Distinct().ToList();
if (something)
{
query = query.Concat(query.Where(some conditions here));
}
else
{
query = query.Concat(query.Where(some other conditions));
}
return query.Distinct().ToList();
Unfortunately in that case query
is null after query.Concat
.
I have also tried:
var subquery = query.Concat(query.Where(some other conditions));
but it returns the same result. Any suggestions?
Upvotes: 2
Views: 3231
Reputation: 22443
.Concat(...)
does a union all. If all you want to do is add a predicate then just swap the current query
with query.Where(...)
IQueryable<Entity1, Entity2> query = //some query with joins
if (something)
{
query = query.Where(some conditions here);
}
else
{
query = query.Where(some other conditions);
}
return query.Distinct().ToList();
... you can even use query syntax.
var query = from e in yourDbContext.YourTable
select e;
if (something)
{
query = from e in query
where someCondition
select e;
}
return query.Distinct().ToList();
Upvotes: 4