This is common to join a table with another on multiple conditions, Like
Select * from Customer c
Join Invoice i on c.CustomerID = i.CustomerID and c.CustomerTypeID = i.CustomerTypeID
I need to write a Linq to SQL query for the same scenario. But the syntax for this query is a bit different to expect. The Linq query is as follows:
From c in DataContext.Customers
Join i in DataContext.Invoices on new { c.CustomerID, c.CustomerTypeID } equals new { i.CustomerID, i.CustomerTypeID }
*DataContext in the above query should be replaced with actual datacontext name.
Select * from Customer c
Join Invoice i on c.CustomerID = i.CustomerID and c.CustomerTypeID = i.CustomerTypeID
I need to write a Linq to SQL query for the same scenario. But the syntax for this query is a bit different to expect. The Linq query is as follows:
From c in DataContext.Customers
Join i in DataContext.Invoices on new { c.CustomerID, c.CustomerTypeID } equals new { i.CustomerID, i.CustomerTypeID }
*DataContext in the above query should be replaced with actual datacontext name.
Hi Thanks for the tip. I am actually for something dynamic joins where the no. of columns may vary for each execution and hence the conditions.
ReplyDeleteCan u please come up with some syntax that solve the purpose.
--Arun
Please do mail me once you got the solution on mail.arun2005@gmail.com
what would be the LINQ syntax for :- Select * from Customer c
ReplyDeleteJoin Invoice i on c.CustomerID = i.CustomerID OR c.CustomerTypeID = i.CustomerTypeID
This code will not work, Anonymous types in linq join needs field name, even better to use a class with IEquatable
ReplyDelete