我打算加入2个列表,并按条形码分组并获得reducedPrice和ItemPrice列的不同值。我应该如何在 Linq 中实现这一目标? (我想要相交结果。我不想要 list2 中的数据,例如 Barcode 003,004,005) 这是我试过的。
var sameJanWithDiffPriceList3 = (from item in list1
join t in list2 on item.BarcodeNo equals t.BarcodeNo
group item by item.BarcodeNo
into grp
select new
{
BarcodeNo = grp.Key,
ReducedPrice = grp.Select(x => x.ReducedPrice).Distinct().Count(),
ItemPrice = grp.Select(x => x.ItemPrice).Distinct().Count()
}).ToList();
条码 | 降价 | 商品价格 |
---|---|---|
000 | 100 | 120 |
000 | 320 | 420 |
001 | 200 | 360 |
001 | 400 | 860 |
002 | 120 | 160 |
099 | 120 | 160 |
条码 | 降价 | 商品价格 | 项目代码 |
---|---|---|---|
000 | 100 | 120 | 00 |
001 | 200 | 360 | 01 |
002 | 120 | 160 | 02 |
003 | 120 | 160 | 02 |
004 | 120 | 160 | 02 |
005 | 120 | 160 | 02 |
条码 | Count(DistinctReducedPrice) | Count(DistinctItemPrice) |
---|---|---|
000 | 2 | 2 |
001 | 3 | 3 |
002 | 1 | 1 |
099 | 1 | 1 |
条码 | Count(DistinctReducedPrice) | Count(DistinctItemPrice) |
---|---|---|
000 | 2 | 2 |
001 | 2 | 2 |
002 | 1 | 1 |
099 | 1 | 1 |
答案 0 :(得分:0)
这应该可以解决问题:
var list2 = new List<(string Barcode,int reducedPrice, int ItemPrice,string ItemCode)>{
("000",100,120,"00"),
("001",200,360,"01"),
("002",120,160,"02"),
("003",120,160,"02"),
("004",120,160,"02"),
("005",120,160,"02")
};
var list1 = new List<(string Barcode,int reducedPrice, int ItemPrice)>{
("000",100,120),
("000",320,420),
("001",200,360),
("001",400,860),
("002",120,160),
("099",120,160)
};
var result = list1.Select(x=>new {x.Barcode,x.reducedPrice,x.ItemPrice})
.Concat(list2.Select(x=>new {x.Barcode,x.reducedPrice,x.ItemPrice}))
.GroupBy(
x=>x.Barcode,
(k,v)=>new {
Barcode=k,
DistinctReducedPrice=v.Select(x=>x.reducedPrice).Distinct().Count(),
DistinctItemPrice=v.Select(x=>x.ItemPrice).Distinct().Count()
}
)
.ToList();
编辑:对于您的新要求(仅交集),您可以稍微修改上面的代码:
var commonKey = list1.Select(x=>x.Barcode).Intersect(list2.Select(x=>x.Barcode)).ToHashSet();
var result = list2.Where(x=>commonKey.Contains(x.Barcode)).Select(x=>new {x.Barcode,x.reducedPrice,x.ItemPrice})
.Concat(list1.Where(x=>commonKey.Contains(x.Barcode)).Select(x=>new {x.Barcode,x.reducedPrice,x.ItemPrice}))
.GroupBy(
x=>x.Barcode,
(k,v)=>new {
Barcode=k,
DistinctReducedPrice=v.Select(x=>x.reducedPrice).Distinct().Count(),
DistinctItemPrice=v.Select(x=>x.ItemPrice).Distinct().Count()
}
)
.ToList();
/*Result:
Barcode DistinctReducedPrice DistinctItemPrice
000 2 2
001 2 2
002 1 1
*/
这还没有解释,如果你想要交集,为什么结果有“099”,因为list2不包含它。另外,为什么您期望“001”为 3 和 3,因为它只有 2 个不同的值(200 和 400;360 和 860)?
答案 1 :(得分:0)
您可以使用以下查询
var sameJanWithDiffPriceList3 = (from item in list1
join t in list2 on item.Barcode equals t.Barcode into joined
from j in joined.DefaultIfEmpty()
select new
{
Barcode = item.Barcode,
reducedPrice = item.reducedPrice + "," + j.reducedPrice,
ItemPrice = item.ItemPrice + "," + j.ItemPrice,
}
into grp
group grp by new { grp.Barcode }
into result
select new
{
Barcode = result.Key,
DistinctReducedPrice = string.Join(",", result.Select(a => a.reducedPrice).ToArray()).ToString().Replace(",0","").Split(',').Distinct().Count(),
DistinctItemPrice = string.Join(",", result.Select(a => a.ItemPrice).ToArray()).ToString().Replace(",0", "").Split(',').Distinct().Count()
}).ToList();