【发布时间】:2021-03-11 12:32:55
【问题描述】:
我计划加入 2 个列表,并按条码分组,并获得 reducePrice 和 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();
列表 1
| Barcode | reducedPrice | ItemPrice |
|---|---|---|
| 000 | 100 | 120 |
| 000 | 320 | 420 |
| 001 | 200 | 360 |
| 001 | 400 | 860 |
| 002 | 120 | 160 |
| 099 | 120 | 160 |
清单 2
| Barcode | reducedPrice | ItemPrice | 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 |
我想要的结果
| Barcode | Count(DistinctReducedPrice) | Count(DistinctItemPrice) |
|---|---|---|
| 000 | 2 | 2 |
| 001 | 3 | 3 |
| 002 | 1 | 1 |
| 099 | 1 | 1 |
我得到的结果
| Barcode | Count(DistinctReducedPrice) | Count(DistinctItemPrice) |
|---|---|---|
| 000 | 2 | 2 |
| 001 | 2 | 2 |
| 002 | 1 | 1 |
| 099 | 1 | 1 |
【问题讨论】:
-
你能告诉我们你已经尝试过什么吗?
-
@Arcord 我编辑了问题。请检查。谢谢。
-
我不知道你尝试过的代码结果,但看起来你只需要使用
Count而不是ToList -
@demo 我用我得到的结果编辑问题。请检查。谢谢。
-
如果你想要交集,为什么结果有“099”,因为list2不包含它?另外,为什么“001”只有 3 个,因为它只有 2 个不同的值(200 和 400)?