开发者

Left join on Linq?

开发者 https://www.devze.com 2023-01-15 02:48 出处:网络
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:

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 t开发者_StackOverflowhe "review" column, but I still want to get the title and the author on my result. How can I achieve this?


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: Linq to Sql: Multiple left outer joins


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"]: ""
                      };
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号