LINQ 怎么查询两个表? RRS feed


  • Navigation property可以在查询里面用的,比如
    from c in Customers where c.Order.TotalAmount>200 select c
    如果没有建立foreign key的话,可以自己join
    from o in db.Orders
        where o.OrderID == orderID
        join s in db.Shippers on o.ShipVia equals s.ShipperID
        join od in db.OrderDetails on o.OrderID equals od.OrderID
        join p in db.Products on od.ProductID equals p.ProductID
        join supplier in db.Suppliers on p.SupplierID equals supplier.SupplierID
        let total = od.Quantity * od.UnitPrice
      select new OrderDescription
          Product = p.ProductName,
          Quantity = od.Quantity,
          ShipperName = s.CompanyName,
          Total = total,
          UnitPrice = od.UnitPrice,
          SupplierName = supplier.CompanyName

    Please mark the post answered your question as the answer, and mark other helpful posts as helpful. This posting is provided "AS IS" with no warranties, and confers no rights.
    Visual C++ MVP
    • 已标记为答案 Ricky.HQB 2009年7月6日 7:34
    2009年7月3日 13:10