We have an entity like this :
public class File{
public int Region{get;set;}
public bool ShowLocation{get;set;}
//Other fields are omitted
}
I would like to write this query:
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
For some reasons I would like to use Linq To Nhibernate (We have a complex filtering mechanism that generates an Expression<Func<File,bool>>)
So far I couldn't find any way to achieve this using Linq To NHibernate. Here's some of my attempts:
Conditioanl Count:(No exception but it count all rows anyway)
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)
});
Conditioanl Sum : Not Supported/Implemented Exception
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)
});
SELECT Before GROUP : Not Supported/Implemented Exception
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 : Not Supported/Implemented Exception
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)
});
I have no other clues. Any other idea ?