tags:

views:

45

answers:

2

I'm actually not sure if this is exactly a left join; I'm not an expert on SQL. I have the following Linq query:

        var title = dataSet.Tables["title"].AsEnumerable();
        var author = dataSet.Tables["author"].AsEnumerable();
        var review = dataSet.Tables["review"].AsEnumerable();

        var results = from t in title
                      join a in author on t["Url"] equals a["Url"]
                      join r in review on t["Url"] equals r["Url"]
                      select new { 
                          tText = t["InnerText"], 
                          aText = a["InnerText"], 
                          rText = r["InnerText"] 
                      };

My problem is that sometimes there is no matching review on the "review" column, but I still want to get the title and the author on my result. How can I achieve this?

+2  A: 

You need a left outer join. http://solidcoding.blogspot.com/2007/12/left-outer-join-in-linq.html

Another example with multiple left outer joins: http://stackoverflow.com/questions/267488/linq-to-sql-multiple-left-outer-joins

Tom Vervoort
A: 

OK this worked, not sure why though:

        var title = dataSet.Tables["title"].AsEnumerable();
        var author = dataSet.Tables["author"].AsEnumerable();
        var review = dataSet.Tables["review"].AsEnumerable();

        var results = from t in title
                      join a in author on t["Url"] equals a["Url"]
                      join r in review on t["Url"] equals r["Url"] into g
                      from r in g.DefaultIfEmpty() 
                      select new { 
                          tText = t["InnerText"], 
                          aText = a["InnerText"], 
                          rText = r != null? r["InnerText"]: ""
                      };
jsoldi