07 April, 2010

Linq: Query for multiple join conditions in Linq to SQL

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.

1 comment:

  1. 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.
    Can u please come up with some syntax that solve the purpose.

    Please do mail me once you got the solution on mail.arun2005@gmail.com