我有来自两个不同表的两个列表;列表来自不同的dbs,我需要连接或创建一个新列表,其中包含两个列表(表)的结果,其中列表2中的值等于列表1的值
表1 ItemList
11| M1 | 1 |Description |x1
22| M3 | 2 |apple |x2
32| M1 | 10 |banana |x3
11| M5 | 30 |Description |x4
表2 ItemSell
11| M1 | 12 |Description |x1
22| M3 | 21 |apple |x2
结果
11| M1 | 11 |Description |x1 | 12
22| M3 | 2 |apple |x2 | 21
32| M1 | 10 |banana | x3 |0
我的代码是:
result.AddRange(
ItemList.Select(x=>x).Distinct()
.Join(
ItemSell.Distinct(),
stock => stock.Material.ToUpper().Trim(),
sell => sell.Material.ToUpper().Trim(),
(stock,sell) => newsellItems
{
Stock=stock.Quantity,
sell=sell.Quantity,
Desc=stock.Desc,
Batch=stock.Batch,
Material=stock.Material,
Order=stock.Order
}
).ToList()
);
使用此代码我只获得这些值
结果
11| M1 | 11 |Description |x1 | 12
22| M3 | 2 |apple |x2 | 21
答案 0 :(得分:1)
你有什么应该工作。我在LINQPad中尝试了一个几乎相同的例子,得到了你期望的结果:
var stockItems = new[]
{
new { Order = 11, Material = "M1", Quantity = 1, Desc = "Description", Batch = "x1" },
new { Order = 22, Material = "M3", Quantity = 2, Desc = "apple", Batch = "x2" },
new { Order = 32, Material = "M1", Quantity = 10, Desc = "banana", Batch = "x3" },
new { Order = 11, Material = "M5", Quantity = 30, Desc = "Description", Batch = "x4" },
};
var sellItems = new[]
{
new { Order = 11, Material = "M1", Quantity = 12, Desc = "Description", Batch = "x1" },
new { Order = 22, Material = "M3", Quantity = 21, Desc = "apple", Batch = "x2" }
};
stockItems
.Distinct()
.Join(
sellItems.Distinct(),
stock => stock.Material.ToUpper().Trim(),
sell => sell.Material.ToUpper().Trim(),
(stock, sell) => new
{
Order = stock.Order,
Material = stock.Material,
Stock = stock.Quantity,
Desc = stock.Desc,
Batch = stock.Batch,
Sell = sell.Quantity
})
.ToList()
.Dump();
结果:
Order | Material | Stock | Desc | Batch | Sell
11 | M1 | 1 | Description | x1 | 12
22 | M3 | 2 | apple | x2 | 21
32 | M1 | 10 | banana | x3 | 12
您的测试一定有问题,或者您没有完全粘贴您的代码。