我目前有两个查询,它们共享相同的复杂查询,我只想执行一次:
//Query 1
from together in (...) // ... = Complex query with multiple joins
where together.property == 0
select new { ... }
//Query 2
from together in (...) // ... = Complex query with multiple joins
where together.property > 0
select new { ... }
问题是,他们有一个不同的where子句。我试图在select语句中设置where子句,但这似乎是可能的,如果我使用groupby
这里我不需要:
//Don't work
from together in (...) // ... = Complex query with multiple joins
select new {
//if together would be grouped, this would work. However I need all data without grouping
// . Together is not IQueryable so this does not work
Foo = together.Where(e => e.property == 0).Select(...),
Bar = together.Where(e => e.property > 0).Select(...)
}
是否可以使用LINQ在一个查询中根据不同的where子句获取2个对象?
答案 0 :(得分:1)
您可以查询所有内容,然后将其拆分,就像这样
var qry= (
from together in (...) // ... = Complex query with multiple joins
where together.property => 0
select together)
.ToList();
var result = new {
Foo = qry.Where(e => e.property == 0).Select(...),
Bar = qry.Where(e => e.property > 0).Select(...)
};