简体   繁体   中英

How can I use this Linq Query using it with a multiple parameter by Join tables

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

is it possible to use this lambda expression linq query with multiple parameters by joining 3 tables?

--- UPDATE STILL ERROR -- :(

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

I made up changes base on the answer but an error said above the statement: "The type arguments for method cannot be inferred from the usage. Try specifying the type arguments explicitly". i think it's talking about the parameters i've made..

You can use anonymous type for the Join like this:

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

The anonymous type will be compiled to use the autoimplemented Equals and GetHashCode so that the equality will be derived by the equality of all the corresponding properties. Just add more properties as you want in the the new {....} . Note that the order of properties provided in the 2 new {...} should be the same order of correspondence. The names should also be matched, you can explicitly specify the names to ensure this (this is needed in some cases) such as:

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

However in your case the property names will be used as the same properties of the item.

UPDATE

This update is just a fix for your specific parameters, I said that the property names should be the same, if they are not you have to explicitly name them like this:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM