1

This is my join in linq I just think is there any possible way to write it with lambda:

enter image description here

Does any one have any idea about this? any suggestion?

4
  • 1
    Yes. If you are unsure where to start, first try a smaller example for practice. Commented Mar 3, 2013 at 14:47
  • 3
    Please copy and paste the code as text rather than taking a screenshot. You've made it much harder for anyone to answer... normally it's useful to be able to copy from the question and edit... Commented Mar 3, 2013 at 14:49
  • 1
    Can you copy and paste the code instead of the image, so i can easily test it? Commented Mar 3, 2013 at 14:53
  • An easy way to translate it would be to install Resharper and let R# do it. Commented Mar 3, 2013 at 15:17

2 Answers 2

7

All LINQ query expressions can be converted into "dot notation" (method calls) - that's basically what the compiler does. However, it introduces transparent identifiers for joins (and some other operations) which make the equivalent code using lambda expressions much more fiddly.

Given that the code would end up being absolutely equivalent to the query expression, I'd suggest you stick with the query expression version.

If you really want to convert to dot notation for some reason, I would strongly advise you to do so with a trivial example first - a single join with just a couple of properties. You'll get a feel for what you need to do, and can gradually build it up.

My Edulinq post on query expression translation gives some details of all the transformations performed by the compiler - it's a good starting point for experimentation.

Sign up to request clarification or add additional context in comments.

Comments

3

Typed it up without an IDE, so surely some mistakes...

join ... on ... equals ... into becomes GroupJoin

from ... in group.DefaultIfEmpty becomes SelectMany( group.DefaultIfEmpty)

Other than that, it's all about managing scope using more anonymous types.

var joinResult1 = FormReportDataTable.AsEnumerable()
  .GroupJoin(dtFormsCategories.AsEnumerable(),
    dr1 => dr1["FormID"], dr2 => dr2["ObjectID"],
    (dr1, dr2s) => new {dr1 = dr1, dr2s = dr2s})
  .SelectMany(g => g.dr2s.DefaultIfEmpty(), (g, dr2) => new {dr1 = g.dr1, dr2 = dr2 })
  .GroupJoin(drEntities.AsEnumerable(),
    x => (Guid)x.dr1["EntityID"], er => (Guid)er["ID"],
    (x, ers) => new {dr1 = x.dr1, dr2 = x.dr2, ers = ers})
  .SelectMany(g => g.ers.DefaultIfEmpty(), (g, er) => new {dr1 = g.dr1, dr2 = g.dr2, er = er })
  .GroupJoin(dtCategories.AsEnumerable(),
    x => (Guid)x.dr2["CategoryID"], cr => (Guid)cr["ID"],
    (x, crs) => new {dr1 = x.dr1, dr2 = x.dr2, er = x.er, crs = crs})
  .SelectMany(g => g.crs.DefaultIfEmpty(), (g, cr) => new {dr1 = g.dr1, dr2 = g.dr2, er = g.er, cr = cr })

var joinResult = joinResult1.Select(x => new
{
  SubPortalName = x.cr == null ? string.Empty :  ...
  ...  //could have posted this code if it was in the question...
});

2 Comments

really makes SQL look easier eh
@ioSamurai if you think that's bad, you haven't seen the SQL I've seen.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.