DefaultIfEmpty and SQL joinsHi.<br>I am struggeling with doing a left outer join. I have 3 tables tblArticle, tblImages and a bridging table called tblArticleImages.<br>I have the LINQ down to retrieve articles with and image as:<br>    from a in tblArticle<br>    join ai in tblArticleImage on a.ID equals ai.articleID<br>    join i in tblImages on ai.imageID equals i.ID<br>    select new {<br>        articleID = a.ID,<br>        imageID = i.ID,<br>        imagePath = i.imagePath<br>    }<br><br>What I now want is to add a defaut imagePath if there is no image that is linked via tblArticleImages. I found some help on DefaultIfEmpty() but just cannot get it to work on 3 tables, the example only had 2 tables. Could someone please try and provide me with the LINQ to do this??<br><br>Then another question, maybe off toppic and it is a newbie question. when I do the select I either select from a, ai or i .... could I not instead of selecting new {....} select from all 3?? Something like select a, ai, i ....??<br><br>Thanks in advance, I am sure if I get the answer some newbie like me will Google this post and find it helpfull.  <br>© 2009 Microsoft Corporation. All rights reserved.Fri, 30 Jan 2009 06:43:37 Z797a6e7a-84fb-49a4-99d2-512f06c4f93ehttp://social.msdn.microsoft.com/Forums/en-US/linqprojectgeneral/thread/797a6e7a-84fb-49a4-99d2-512f06c4f93e#797a6e7a-84fb-49a4-99d2-512f06c4f93ehttp://social.msdn.microsoft.com/Forums/en-US/linqprojectgeneral/thread/797a6e7a-84fb-49a4-99d2-512f06c4f93e#797a6e7a-84fb-49a4-99d2-512f06c4f93eSarel jhttp://social.msdn.microsoft.com/Profile/en-US/?user=Sarel%20jDefaultIfEmpty and SQL joinsHi.<br>I am struggeling with doing a left outer join. I have 3 tables tblArticle, tblImages and a bridging table called tblArticleImages.<br>I have the LINQ down to retrieve articles with and image as:<br>    from a in tblArticle<br>    join ai in tblArticleImage on a.ID equals ai.articleID<br>    join i in tblImages on ai.imageID equals i.ID<br>    select new {<br>        articleID = a.ID,<br>        imageID = i.ID,<br>        imagePath = i.imagePath<br>    }<br><br>What I now want is to add a defaut imagePath if there is no image that is linked via tblArticleImages. I found some help on DefaultIfEmpty() but just cannot get it to work on 3 tables, the example only had 2 tables. Could someone please try and provide me with the LINQ to do this??<br><br>Then another question, maybe off toppic and it is a newbie question. when I do the select I either select from a, ai or i .... could I not instead of selecting new {....} select from all 3?? Something like select a, ai, i ....??<br><br>Thanks in advance, I am sure if I get the answer some newbie like me will Google this post and find it helpfull.  <br>Tue, 13 May 2008 18:47:38 Z2008-05-19T02:36:38Zhttp://social.msdn.microsoft.com/Forums/en-US/linqprojectgeneral/thread/797a6e7a-84fb-49a4-99d2-512f06c4f93e#2418f278-583c-4d68-bbe8-b6c7cbf0e544http://social.msdn.microsoft.com/Forums/en-US/linqprojectgeneral/thread/797a6e7a-84fb-49a4-99d2-512f06c4f93e#2418f278-583c-4d68-bbe8-b6c7cbf0e544Martin Guralnikhttp://social.msdn.microsoft.com/Profile/en-US/?user=Martin%20GuralnikDefaultIfEmpty and SQL joins<p align=left>Here's a possible implementation:</p> <p align=left> </p> <p align=left> <div class=codeseg> <div class=codecontent> <div class=codesniptitle><span style="width:100%">Code Snippet</span></div> <p align=left>var query = from a in tblArticle</p> <blockquote dir=ltr style="margin-right:0px"> <p align=left>join image in (</p> <blockquote dir=ltr style="margin-right:0px"> <p align=left>from ai in tblArticleImage</p> <p align=left>join i in tblImages on ai.imageID equals i.ID</p> <p align=left>select new</p> <p align=left>{</p> <blockquote dir=ltr style="margin-right:0px"> <p align=left>articleID = ai.articleID,</p> <p align=left>imageID = i.ID,</p> <p align=left>imagePath = i.imagePath</p></blockquote> <p align=left>}) on a.articleID equals image.articleID into joinedImages</p></blockquote> <p align=left>from defaultIfEmptyImage in joinedImages.DefaultIfEmpty()</p> <p align=left>select new</p> <p align=left>{</p> <blockquote dir=ltr style="margin-right:0px"> <p align=left>articleID = a.articleID,</p> <p align=left>imageID = defaultIfEmptyImage.imageID,</p> <p align=left>imagePath = defaultIfEmptyImage.imagePath</p></blockquote> <p align=left>};</p></blockquote> <p align=left> </p></div></div> <p align=left> </p> <p></p> <blockquote dir=ltr style="margin-right:0px"> <p align=left> </p></blockquote>Fri, 16 May 2008 19:13:55 Z2008-05-19T02:36:38Zhttp://social.msdn.microsoft.com/Forums/en-US/linqprojectgeneral/thread/797a6e7a-84fb-49a4-99d2-512f06c4f93e#344ab6a5-6029-4643-a391-10fff4ccf77ahttp://social.msdn.microsoft.com/Forums/en-US/linqprojectgeneral/thread/797a6e7a-84fb-49a4-99d2-512f06c4f93e#344ab6a5-6029-4643-a391-10fff4ccf77aSarel jhttp://social.msdn.microsoft.com/Profile/en-US/?user=Sarel%20jDefaultIfEmpty and SQL joinsThat one might work but my solution for <br> <div class=entry> <p>tblArticle, tblArticleImages and tblImages that is as follows might work better(Or I read it easier??)</p><br> <p><br></p> <p><br></p> <p>from a in tblArticles<br>join ac in tblArticleCategories on a.ID equals ac.articleID<br>join c in tblCategories on ac.categoryID equals c.ID<br>where c.name == “Africa insight”<br>select new<br>{<br>categoryName = c.name,<br>headline = a.heading,<br>articleID = a.ID,<br>synopsis = a.synopsis,<br>story = a.story,<br>imagePath = (<br>from a2 in tblArticles<br>join ai in tblArticleImages on a2.ID equals ai.articleID<br>join i in tblImages on ai.imageID equals i.ID<br>where i.isThumbnail == true &amp;&amp; a2.ID == a.ID<br>select i.imagePath<br>).FirstOrDefault()<br>}</p></div>Mon, 19 May 2008 19:29:13 Z2008-05-19T19:29:13Z