1

I'm trying to translate following query to lambda expression

var results = (from g in _groups
         join cr in _categoryRoots on g.Id equals cr.Group_Id into gcr
         from lgc in gcr.DefaultIfEmpty(new CategoryRoot())
         join c in _categories on lgc.Id equals c.CategoryRoot_Id into ccr
         from lccr in ccr.DefaultIfEmpty()
         select new GroupCategory
         {
             Id = g.Id,
             Name = g.Name,
             CategoryRoot_Id = lgc == null ? 0: lgc.Id,
             CategoryRootName = lgc == null ? "": lgc.Name,
             Category_Id = lccr == null ? 0: lccr.Id,
             CategoryName = lccr == null ? "": lccr.Name
         }).ToList();

Want to change in to something like below.

var rs = _groups.Join(_categoryRoots, g => g.Id, cr => cr.Group_Id, (g, cr) => new GroupCategory { Id = g.Id, Name = g.Name, CategoryRoot_Id = cr.Id, CategoryRootName = cr.Name  });

running code example https://dotnetfiddle.net/A3AO0V

Peter
  • 5,455
  • 7
  • 46
  • 68
Tauha
  • 99
  • 1
  • 12
  • 2
    So, what is the question? Or is it just a demonstration? – Icepickle Oct 17 '17 at 16:26
  • By lambda expression do you mean method syntax as in `_groups.Join(.....`? – juharr Oct 17 '17 at 16:30
  • Why do you need lambda syntax? Joins are notoriously nasty in lambda syntax, and left joins even more so. – D Stanley Oct 17 '17 at 16:31
  • question is clear with working example, i have 3 tables that i want to left join through lambda expression, @juharr understood it. I also agree with @D Stanley if lambda expression is not a good choice for such query its the right answer, I was able to get output what i wanted, but i was not able to learn how could i translate into lambda, just wanted to learn. – Tauha Oct 17 '17 at 16:47
  • I just want to reiterate it's METHOD SYNTAX, not lambda expression or lambda syntax. – juharr Oct 17 '17 at 16:51
  • @juharr got it, thanks for correction – Tauha Oct 17 '17 at 16:57
  • [Here](https://stackoverflow.com/a/1525343/1081897) is an example on converting a left join from query syntax to method syntax. As you can see it doesn't really clarify things. In fast, it arguable makes the _more_ complicated. – D Stanley Oct 17 '17 at 17:00
  • Linqpad can do this for you. – Gert Arnold Oct 17 '17 at 22:42

1 Answers1

0

I don't understand either what you really ask, but I just wanted to share a way to simplify your query. The left joins are a lot less messy if done this way:

var results = (from g in _groups
               from lgc in _categoryRoots.Where(cr => g.Id == cr.Group_Id).DefaultIfEmpty(new CategoryRoot())
               from lccr in _categories.Where(c => lgc.Id == c.CategoryRoot_Id).DefaultIfEmpty()
               select new GroupCategory
               {
                   Id = g.Id,
                   Name = g.Name,
                   CategoryRoot_Id = lgc?.Id ?? 0,
                   CategoryRootName = lgc?.Name ?? "",
                   Category_Id = lccr?.Id ?? ',
                   CategoryName = lccr?.Name ?? ""
               }).ToList();

And also if you are using at least C# 6 (VS 2015) you can take advantage of the null coalescing operator ?. to simplify those null checks.

Andrew
  • 7,602
  • 2
  • 34
  • 42
  • sorry for my bad English, I updated question. do you agree with @D Stanley that lambda syntax will make it unnecessarily complex? just wanted to simplify the query, as you gave some thought on it. – Tauha Oct 17 '17 at 16:57
  • I think the way it's in my answer is much more clear and readable than with method syntax (like that `_groups.Join(_categoryRoots...` you added in your question). Don't you agree? – Andrew Oct 17 '17 at 17:16