How to write LINQ query for Inner Join with OR condition?

Sometimes, you need to apply inner join with or condition. To write query for inner join with or condition you need to use || operator in where condition as shown below:

DataContext context = new DataContext();
var q = from cust in context.tblCustomer from ord in context.tblOrder where(cust.CustID == ord.CustomerID || cust.ContactNo == ord.ContactNo) select new {
	cust.Name, cust.Address, ord.OrderID, ord.Quantity
};

// Generated SQL

SELECT
[t0].[Name],
[t0].[Address],
[t1].[OrderID],
[t1].[Quantity]
FROM
[tblCustomer] AS [t0],
[tblOrder] AS [t1]
WHERE
(
([t0].[CustID]) = [t1].[CustomerID]
)
OR (
[t0].[ContactNo] = [t1].[ContactNo]
)
Tagged , . Bookmark the permalink.

Leave a Reply