我有以下内容:
IQueryable<Entity1, Entity2> query = //some query with joins
return query.Distinct().ToList();
这很好。我想做的是通过向查询添加一些其他部分来扩展查询,例如:
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();
不幸的是,在这种情况下,query
之后为query.Concat
为空。
我也尝试过:
var subquery = query.Concat(query.Where(some other conditions));
,但返回相同的结果。有什么建议吗?
答案 0 :(得分:1)
.Concat(...)
进行全部并集。如果您只想添加一个谓词,则只需将当前的query
与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();
...您甚至可以使用查询语法。
var query = from e in yourDbContext.YourTable
select e;
if (something)
{
query = from e in query
where someCondition
select e;
}
return query.Distinct().ToList();