I have a SQL query and I am trying to convert it to LINQ. But when I do so, I am getting this error:
SQL cannot be converted to LINQ: DISTINCT an Agregate count cannot be converted to LINQ
SELECT
ST_CN_NO,
DN_DT,
count(ST_CN_NO) as totlines,
count(distinct LOC_CODE) as totloc,
sum(SN_QTY)as totscnqty,
sum(SAP_QTY) as totmpqty
from physical_count
where 1=1
group by ST_CN_NO,DN_DT
order by physical_count.ST_CN_NO
So, how can I convert count(distinct LOC_CODE)
to LINQ? Is there any way to do so?
This is my LINQ query whithout adding count(distinct LOC_CODE)
:
from t in db.PHYSICAL_COUNT
where
1 == 1
group t by new {
t.ST_CN_NO,
t.DN_DT
} into g
orderby
g.Key.ST_CN_NO
select new {
ST_CN_NO = (Int32?)g.Key.ST_CN_NO,
g.Key.DN_DT,
totlines = (Int64?)g.Count(),
totscnqty = (Int32?)g.Sum(p => p.SN_QTY),
totmpqty = (Int32?)g.Sum(p => p.SAP_QTY)
}