2

我如何在 linq 中编写此查询?

select * from bills as b inner join customer as c1
          On b.shipperID=c1.CustomerID inner join customer c2
          On b.ConsigneeID=c2.CustomerID      
---------------------------

我需要如下:

var result=from p1 in entities.bills
           join p2 in entities.customer on p1.shipperID equals p2.customerID
           join p3 in entities.customer on p1.consigneeID equals p3.customerID
           select p2;
           return resuls.Tolist()

谢谢:)

4

1 回答 1

5

在您的 SQL 中,您正在选择所有,因此在 linq 中您需要将所有对象放入新的匿名类型中,如下所示。

var result = from p1 in entities.bills
             join p2 in entities.customer on p1.shipperID equals p2.customerID
             join p3 in entities.customer on p1.consigneeID equals p3.customerID
             select new 
                 { 
                     Bills = p1,
                     Shippers = p2,
                     Consignees = p3
                 };

             return resuls.Tolist();

或者,如果您需要将它们展平,则必须逐个属性地投影它们。

您应该在 LINQ 中使用导航属性,例如

from bills in entities.bills
select new
{
    bills.Shipper
    bills.Consignee
};
于 2013-05-28T20:13:14.810 回答