【发布时间】:2015-04-28 09:19:45
【问题描述】:
我们有这样的实体:
public class File{
public int Region{get;set;}
public bool ShowLocation{get;set;}
//Other fields are omitted
}
我想写这个查询:
SELECT Region,SUM(CASE WHEN ShowLocation=1 THEN 1 ELSE 0 END) AS
ShowCount,SUM(CASE WHEN ShowLocation=0 THEN 1 ELSE 0 END) AS NotShowCount
--WHERE omitted for the sake of simplicity
GROUP BY Region
出于某些原因,我想使用 Linq To Nhibernate(我们有一个复杂的过滤机制,可以生成 Expression<Func<File,bool>>)
到目前为止,我找不到使用 Linq To NHibernate 实现此目的的任何方法。 以下是我的一些尝试:
条件计数:(没有例外,但它仍然计算所有行)
Files
.Where(whereExpression)
.GroupBy(x=>x.Region)
.Select(x=>new
{
x.Region,
ShowCount=x.Count(f=>f.ShowLocation==1),
NotShowCount=x.Count(f=>f.ShowLocation==0)
});
条件总和:不支持/实现的异常
Files
.Where(whereExpression)
.GroupBy(x=>x.Region)
.Select(x=>new
{
x.Region,
ShowCount=x.SUM(f=>f.ShowLocation==1?1:0),
NotShowCount=x.SUM(f=>f.ShowLocation==0?1:0)
});
在 GROUP 之前选择:不支持/实现的异常
Files.Where(whereExpression).Select(x=>new
{
x.Region,
Show=x.ShowLocation==1?1:0,
NotShow=x.ShowLocation==0?1:0
})
.GroupBy(x=>x.Region)
.Select(x=>new
{
x.Region,
ShowCount=x.SUM(f=>f.Show),
NotShowCount=x.SUM(f=>f.NotShow)
});
UNION : 不支持/实现的异常
Files
.Where(whereExpression)
.Where(x=>x.ShowLocation==1)
.Select(x=>new
{
x.Region,
Show=1,NotShow=0
})
.Union(Files
.Where(whereExpression)
.Where(x=>x.ShowLocation==0)
.Select(x=>new
{x.Region,
Show=0,
NotShow=1
}))
.GroupBy(x=>x.Region)
.Select(x=>new
{
x.Region,
CountShow=x.Count(a=>a.Show),
CountNotShow=x.Count(a=>a.NotShow)
});
我没有其他线索。还有什么想法吗?
【问题讨论】:
标签: c# linq nhibernate