如何使用此连接表使用多个参数的Linq查询

时间:2013-11-23 02:45:41

标签: c# .net sql linq

var list = dc.Orders.
            Join(dc.Order_Details,
            o => o.OrderID, od => od.OrderID, <-- what if i have 2 more parameters let say an ID and a REC on both table. ex.: o=> o.OrderID && o.ItemName, od => od.OrderID && od.Itemname then (o, od) but its result is error? is there another way?
            (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, <-- at this point too?
                    (a, p) => new
                    {
                        OrderID = a.OrderID,
                        OrderDate = a.OrderDate,
                        ShipName = a.ShipName,
                        Quantity = a.Quantity,
                        UnitPrice = a.UnitPrice,
                        ProductName = p.ProductName
                    });

是否可以通过连接3个表来使用带有多个参数的lambda表达式linq查询?

---更新仍然错误 - :(

var header = DB.Delivery_HeaderRECs.Join(DB.Delivery_DetailsRECs, <-- Red Line on DB.Delivery_HeaderRECs.Join
                                q => new { q.drNO, q.RecNO },
                                qw => new { qw.DrNO, qw.RecNO },
                                (q, qw) => new
                                {
                                    DR = q.drNO,
                                    DATE = q.DocDate,
                                    RECNO = q.RecNO,
                                    CUSTID = q.CustomerID,
                                    CUSTADDR = q.CustomerADDR,
                                    RELEASE = q.ReleasedBy,
                                    RECEIVE = q.ReceivedBy,
                                    REMARKS = q.Remarks,

                                    ITEM = qw.ItemCode,
                                    DESC = qw.ItemDesc,
                                    QTY = qw.Qty,
                                    COST = qw.Unit,
                                    PLATENO = qw.PlateNo,
                                    TICKETNO = qw.TicketNo
                                }).Join(DB.Delivery_TruckScaleRECs,
                                w => new { w.DR, w.TICKETNO },
                                we => new { we.DrNo, we.TicketNO },
                                (w, we) => new 
                                {
                                    DR = w.DR,
                                    DATE = w.DATE,
                                    RECNO = w.RECNO,
                                    CUSTID = w.CUSTID,
                                    CUSTADDR = w.CUSTADDR,
                                    RELEASE = w.RELEASE,
                                    RECEIVE = w.RECEIVE,
                                    REMARKS = w.REMARKS,

                                    ITEM = w.ITEM,
                                    DESC = w.DESC,
                                    QTY = w.QTY,
                                    COST = w.COST,
                                    PLATENO = w.PLATENO,
                                    TICKETNO = w.TICKETNO,

                                    TRANSAC = we.TransactionType,
                                    FWEIGHT = we.FirstWeight,
                                    SWEIGHT = we.SecondWeight,
                                    NWEIGHT = we.NetWeight
                                }).FirstOrDefault();

我根据答案进行了更改,但在声明上方出现了错误: “无法从用法中推断出方法的类型参数。请尝试明确指定类型参数”。我认为它正在谈论我所做的参数..

1 个答案:

答案 0 :(得分:1)

您可以像这样使用匿名类型:

var list = dc.Orders.Join(dc.Order_Details,
                          o => new { o.OrderID, o.ItemName}, 
                          od => new { od.OrderID, od.ItemName},
                          ...);

将编译匿名类型以使用自动实现的EqualsGetHashCode,以便通过所有相应属性的相等来派生相等性。只需在new {....}中添加更多属性即可。请注意,2 new {...}中提供的属性顺序应该是相同的对应顺序。名称也应该匹配,您可以明确指定名称以确保这一点(在某些情况下需要这样做),例如:

new {OrderID = o.OrderID, Name = o.ItemName}

但是在您的情况下,属性名称将用作项目的相同属性。

<强>更新

此更新只是针对您的特定参数的修复,我说属性名称应该相同,如果它们不是您必须明确地将它们命名为:

var list = dc.Orders.Join(dc.Order_Details,
                           q => new {DrNO =  q.drNO, q.RecNO}, 
                           qw => new {DrNO = qw.DrNO, qw.RecNO},
                           ...);