LINQ to SQL: Multiple joins ON multiple Columns. Is this possible?

Joining on multiple columns in Linq to SQL is a little different var query = from t1 in myTABLE1List // List join t2 in myTABLE1List on new { t1. ColumnA, t1. ColumnB } equals new { t2.

ColumnA, t2. ColumnB } You have to take advantage of anonymous types and compose a type for the multiple columns you wish to compare against This seems confusing at first but once you get acquainted with the way the SQL is composed from the expressions it will make a lot more sense, under the covers this will generate the type of join you are looking for EDIT Adding example for second join based on comment var query = from t1 in myTABLE1List // List join t2 in myTABLE1List on new { t1. ColumnA, t1.

ColumnB } equals new { t2. ColumnA, t2. ColumnB } join t3 in myTABLE1List on new { t2.

ColumnA, t2. ColumnB } equals new { t3. ColumnA, t3.

ColumnB }.

Joining on multiple columns in Linq to SQL is a little different. Var query = from t1 in myTABLE1List // List join t2 in myTABLE1List on new { t1. ColumnA, t1.

ColumnB } equals new { t2. ColumnA, t2. ColumnB } ... You have to take advantage of anonymous types and compose a type for the multiple columns you wish to compare against.

This seems confusing at first but once you get acquainted with the way the SQL is composed from the expressions it will make a lot more sense, under the covers this will generate the type of join you are looking for. EDIT Adding example for second join based on comment. Var query = from t1 in myTABLE1List // List join t2 in myTABLE1List on new { t1.

ColumnA, t1. ColumnB } equals new { t2. ColumnA, t2.

ColumnB } join t3 in myTABLE1List on new { t2. ColumnA, t2. ColumnB } equals new { t3.

ColumnA, t3. ColumnB } ...

This works great for two joins. I need it to work with THREE joins. Sorry, the second code block was a little misleading.

– DJTripleThreat Mar 15 at 5:31 Thanks for the edit! :D – DJTripleThreat Mar 15 at 5:41 @DJTripleThreat You bet! – Quintin Robinson Mar 15 at 5:41.

In LINQ2SQL you seldom need to join explicitly when using inner joins. If you have proper foreign key relationships in your database you will automatically get a relation in the LINQ designer (if not you can create a relation manually in the designer, although you should really have proper relations in your database) Then you can just access related tables with the "dot-notation" var q = from child in context. Childs where child.Parent.

Col2 == 4 select new { childCol1 = child. Col1, parentCol1 = child.Parent. Col1, }; will generate the query SELECT t0.

Col1 AS childCol1, t1. Col1 AS parentCol1 FROM dbo. Child AS t0 INNER JOIN dbo.

Parent AS t1 ON (t1. Col1 = t0. Col1) AND (t1.

Col2 = t0. Col2) WHERE t1. Col2 = @p0 -- @p0: Input Int (Size = -1; Prec = 0; Scale = 0) 4 -- Context: SqlProvider(Sql2008) Model: AttributedMetaModel Build: 4.0.30319.1 In my opinion this is much more readable and lets you concentrate on your special conditions and not the actual mechanics of the join.

Edit This is of course only applicable when you want to join in the line with our database model. If you want to join "outside the model" you need to resort to manual joins as in the answer from Quintin Robinson.

You can write your query like this. Var query = from t1 in myTABLE1List // List join t2 in myTABLE1List on t1. ColumnA equals t2.

ColumnA and t1. ColumnB equals t2. ColumnA If you want to compare your column with multiple columns.

1 that's not valid syntax (AND is not allowed there...) – Ben M Mar 15 at 5:23 1 @user658720 Welcome to StackOverFlow :). I would suggest that you format you code so that its easier to read. You can select the text and click the code button on the editor.

– DJTripleThreat Mar 15 at 5:28 thanks for your suggestion. – Anvesh Mar 16 at 4:56.

Parent AS t1 ON (t1. Col1 = t0. Col1) AND (t1.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions