Reputation: 3
I have two lists from two different tables; the lists are from different dbs and I need to join or create a new list with the result of the two list(tables), where the value from list 2 are equals to the values of the list 1
Table 1 ItemList
11| M1 | 1 |Description |x1
22| M3 | 2 |apple |x2
32| M1 | 10 |banana |x3
11| M5 | 30 |Description |x4
Table 2 ItemSell
11| M1 | 12 |Description |x1
22| M3 | 21 |apple |x2
Result
11| M1 | 11 |Description |x1 | 12
22| M3 | 2 |apple |x2 | 21
32| M1 | 10 |banana | x3 |0
My code is this:
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()
);
With this code I only get these values
Result
11| M1 | 11 |Description |x1 | 12
22| M3 | 2 |apple |x2 | 21
Upvotes: 0
Views: 142
Reputation: 42394
What you have should work. I tried a nearly identical example in LINQPad and got the results you were expecting:
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();
Result:
Order | Material | Stock | Desc | Batch | Sell
11 | M1 | 1 | Description | x1 | 12
22 | M3 | 2 | apple | x2 | 21
32 | M1 | 10 | banana | x3 | 12
There must be something wrong with your test, or you haven't pasted in exactly the code you have.
Upvotes: 1