0
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..

2
  • What do you mean by not on both table? looks like those 2 parameters are some kind of outside variables? Commented Nov 23, 2013 at 2:47
  • sorry about that sir.. my mistake i have fixed it already. Commented Nov 23, 2013 at 2:55

1 Answer 1

1

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},
                           ...);
Sign up to request clarification or add additional context in comments.

8 Comments

it has an error sir... i'll paste the whole code it says "the type arguments for method cannot be inferred from the usage try specifying the type arguments explicitly"
@Oblivious7 could you post the whole code you tried? I'm sure there should not any error, BTW the ... is what you have to fill yourself. your o and od of course should have ItemName, it's just an example.
i update already the codes sir.. you can check it above i put a label at it updated error..
@Oblivious7 please confirm this q.drNO, is that what you have or it's q.DrNO? Note that case-sensitivity is regarded. BTW, pleas re-read my answer, the corresponding property names should be the same
does it matter if on my Table 1 its drNO and in Table 2 its DrNO? must it suppose to be the same format?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.