SQL到LINQ:使用group by,join,where with count

时间:2015-10-16 10:11:15

标签: sql-server linq join group-by

我有这样的SQL:

select p.ProductID, count(pa.Value) as condition
from ProductBE as p
join ProductAttributeBE as pa 
on p.ProductID = pa.ProductID
where ProductCategoryID = 1203
and (Value = 'PassingerCar' or Value = '15')
group by p.ProductID

使用ProductAttributeBE,ProductBE是一对一的 什么认为接缝是这样的:

var query1 = from p in this.DataBase.ProductBEs
             join a in this.DataBase.ProductAttributeBEs
             on p.ProductID equals a.ProductID
             where  p.ProductCategoryID == 1203 && (a.Value == radius ||                    a.Value == carType) 
             group p by p.ProductID into grp
             select new { grp.Key, Condition = grp.Count(x => x.Value !=   null) };

此代码未编译 从不同的表/别名中选择或分组有问题

1 个答案:

答案 0 :(得分:0)

试试这个:

var query1 = from p in this.DataBase.ProductBEs
             join a in this.DataBase.ProductAttributeBEs
             on p.ProductID equals a.ProductID
             where  p.ProductCategoryID == 1203 && (a.Value == radius || a.Value == carType) 
             select new {p.ProductID, a.Value} into t
             group t by t.ProductID into grp
             select new { grp.Key, Condition = grp.Count(x => x.Value != null) };