如何左连接到另一个左连接
How to left join to another left join
我有一个 table 用来存放库存物品。这些库存商品具有以下属性
public class StockItem
{
public int BrandId { get; set; }
public int ModelId { get; set; }
public decimal Price { get; set; }
public int StoreId { get; set; }
}
其中 StoreId 可以是 1(Store A)或 2(Store B)
我想对这些项目进行分组以获得以下结果
BrandId
ModelId
Count in Store A
count in Store B
我以前用过group by
子句,但不是这样。我应该如何编写 linQ 语句来完成我的任务?
总结一下我有以下记录
Id BrandId ModelId Price StoreId
=========================================
1 1 1 11 1
2 1 1 11 1
3 1 2 12 1
4 1 2 12 2
5 1 1 11 2
并试图获得以下结果
BrandId ModelId CountInStoreA CountInStoreB
=================================================
1 1 2 1
1 2 1 1
var result = from item in db.Items
group item by new { item.BrandId, item.ModelId, item.TotalPrice }
into gr
select new
{
gr.Key.BrandId,
gr.Key.ModelId,
gr.Key.TotalPrice,
CountA = gr.Count(it => it.StoreId == 1),
CountB = gr.Count(it => it.StoreId == 2),
}
我有一个 table 用来存放库存物品。这些库存商品具有以下属性
public class StockItem
{
public int BrandId { get; set; }
public int ModelId { get; set; }
public decimal Price { get; set; }
public int StoreId { get; set; }
}
其中 StoreId 可以是 1(Store A)或 2(Store B)
我想对这些项目进行分组以获得以下结果
BrandId
ModelId
Count in Store A
count in Store B
我以前用过group by
子句,但不是这样。我应该如何编写 linQ 语句来完成我的任务?
总结一下我有以下记录
Id BrandId ModelId Price StoreId
=========================================
1 1 1 11 1
2 1 1 11 1
3 1 2 12 1
4 1 2 12 2
5 1 1 11 2
并试图获得以下结果
BrandId ModelId CountInStoreA CountInStoreB
=================================================
1 1 2 1
1 2 1 1
var result = from item in db.Items
group item by new { item.BrandId, item.ModelId, item.TotalPrice }
into gr
select new
{
gr.Key.BrandId,
gr.Key.ModelId,
gr.Key.TotalPrice,
CountA = gr.Count(it => it.StoreId == 1),
CountB = gr.Count(it => it.StoreId == 2),
}