HI WELCOME TO SIRIS

LINQ Inner Join with AND and OR condition

LINQ has a JOIN query operator that provides SQL JOIN like behavior and syntax. As you know, Inner join returns only those records or rows that match or exists in both the tables. The simple inner join example is given below:
  1. DataContext context = new DataContext();
  2. var q = (from pd in context.Products
  3. join od in context.Orders on pd.ProductID equals od.ProductID
  4. orderby od.OrderID
  5. select new
  6. {
  7. od.OrderID,
  8. pd.ProductID,
  9. pd.Name,
  10. pd.UnitPrice,
  11. od.Quantity,
  12. od.Price,
  13. }).ToList();

Inner Join with AND condition

Sometimes, you need to apply inner join with and condition. To write query for inner join with and condition you need to make two anonymous types (one for left table and one for right table) by using new keyword and compare both the anonymous types as shown below:
  1. DataContext context = new DataContext();
  2. var q=from cust in context.tblCustomer
  3. join ord in context.tblOrder
  4. // Both anonymous types should have exact same number of properties having same name and datatype
  5. on new {a=(int?)cust.CustID, cust.ContactNo} equals new {a=ord.CustomerID, ord.ContactNo}
  6. select new
  7. {
  8. cust.Name,
  9. cust.Address,
  10. ord.OrderID,
  11. ord.Quantity
  12. };
  1. // Generated SQL
  2. SELECT [t0].[Name], [t0].[Address], [t1].[OrderID], [t1].[Quantity]
  3. FROM [tblCustomer] AS [t0]
  4. INNER JOIN [tblOrder] AS [t1] ON (([t0].[CustID]) = [t1].[CustomerID]) AND ([t0].[ContactNo] = [t1].[ContactNo])

Note

  1. Always remember, both the anonymous types should have exact same number of properties with same name and Datatype otherwise you will get the compile time error "Type inference failed in the call to Join".
  2. Both the comparing fields should define either NULL or NOT NULL values.
  3. If one of them is defined NULL and other is defined NOT NULL then we need to do typecasting of NOT NULL field to NULL data type like as above

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:
  1. DataContext context = new DataContext();
  2. var q=from cust in context.tblCustomer
  3. from ord in context.tblOrder
  4. where (cust.CustID==ord.CustomerID || cust.ContactNo==ord.ContactNo)
  5. select new
  6. {
  7. cust.Name,
  8. cust.Address,
  9. ord.OrderID,
  10. ord.Quantity
  11. };
  1. // Generated SQL
  2. SELECT [t0].[Name], [t0].[Address], [t1].[OrderID], [t1].[Quantity]
  3. FROM [tblCustomer] AS [t0], [tblOrder] AS [t1]
  4. WHERE (([t0].[CustID]) = [t1].[CustomerID]) OR ([t0].[ContactNo] = [t1].[ContactNo])
What do you think?
I hope you will enjoy LINQ query with AND and OR conditions while programming with LINQ. I would like to have feedback from my blog readers. Your valuable feedback, question, or comments about this article are always welcome.