我正在尝试使用 LINQ 查询此表:
这是我想要做的:
这是我的 LINQ 查询:
var query = from a in table
where a.Country.Equals("USA")
group a by a.Product_brand into grp
select new
{
Product_brand = grp.key.Product_brand,
Country = grp.Key.Country,
Black = grp.Count(a => a.Black=="Yes"),
White = grp.Count(a => a.White=="Yes"),
Red = grp.Count(a=> a.Red=="Yes"),
Green = grp.Count(a=> a.Green=="Yes")
}
我不知道我的查询出了什么问题,我不断收到这条消息:
替代解决方案:
sql查询:
SELECT [Product brand], Country,
sum(case when [Black] = 'Yes' then 1 else 0 end) as Black,
sum(case when [White] = 'Yes' then 1 else 0 end) as White,
sum(case when [Red] = 'Yes' then 1 else 0 end) as Red,
sum(case when [Green] = 'Yes' then 1 else 0 end) as Green,
FROM dbo.Table
group by [Product brand], Country