Quelqu'un peut-il me montrer deux exemples simples pour joindre des tables 2 et 3 à l'aide de LAMBDA EXPRESSION(
par exemple en utilisant des tables Northwind (Orders, CustomerID, EmployeeID)?
Le code pour joindre 3 tables est:
var list = dc.Orders.
Join(dc.Order_Details,
o => o.OrderID, od => od.OrderID,
(o, od) => new
{
OrderID = o.OrderID,
OrderDate = o.OrderDate,
ShipName = o.ShipName,
Quantity = od.Quantity,
UnitPrice = od.UnitPrice,
ProductID = od.ProductID
}).Join(dc.Products,
a => a.ProductID, p => p.ProductID,
(a, p) => new
{
OrderID = a.OrderID,
OrderDate = a.OrderDate,
ShipName = a.ShipName,
Quantity = a.Quantity,
UnitPrice = a.UnitPrice,
ProductName = p.ProductName
});
Merci
essayez celui-ci pour joindre 2 tables en utilisant l'expression lambda
var list = dataModel.Customers
.Join( dataModel.Orders,
c => c.Id,
o => o.CustomerId,
(c, o) => new
{
CustomerId = c.Id,
CustomerFirstName = c.Firstname,
OrderNumber = o.Number
});
public void Linq102()
{
string[] categories = new string[]{
"Beverages",
"Condiments",
"Vegetables",
"Dairy Products",
"Seafood" };
List<Product> products = GetProductList();
var q =
from c in categories
join p in products on c equals p.Category
select new { Category = c, p.ProductName };
foreach (var v in q)
{
Console.WriteLine(v.ProductName + ": " + v.Category);
}
}