使用lambda表达式连接2和3表的简单示例



谁能给我举两个使用LAMBDA EXPRESSION(连接2和3表的简单例子?
例如,使用Northwind表(Orders,CustomerID,EmployeeID)?

连接3个表的代码为:

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
                        });

谢谢

尝试使用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);  
} 
}

相关内容

  • 没有找到相关文章