【问题标题】:SQL to LINQ: using group by, join, where with countSQL to LINQ:使用 group by、join、where with count
【发布时间】:2015-10-16 10:11:14
【问题描述】:

我有这样的 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

ProductBE 与 ProductAttributeBE 是一对多 什么觉得它看起来像这样:

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) };

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

【问题讨论】:

    标签: sql-server linq join group-by


    【解决方案1】:

    试试这个:

    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) };
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-12-31
      • 2011-04-18
      • 2010-12-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多