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

发布于 2024-11-04 08:47:42 字数 108 浏览 0 评论 0原文

任何人都可以向我展示使用 LAMBDA EXPRESSION(
连接 2 和 3 表的两个简单示例 例如使用 Northwind 表(订单、客户 ID、员工 ID)?

Can anyone show me two simple examples of joining 2 and 3 tables using LAMBDA EXPRESSION(
for example using Northwind tables (Orders,CustomerID,EmployeeID)?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

臻嫒无言 2024-11-11 08:47:42

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

谢谢

Code for joining 3 tables is:

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

Thanks

踏雪无痕 2024-11-11 08:47:42

尝试使用 lambda 表达式连接 2 个表

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

try this one to join 2 tables using lambda expression

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
                 });
孤星 2024-11-11 08:47:42
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);  
} 
}
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);  
} 
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文