5

I am trying to write the following Linq (query style) in Lambda Expression so that I can chain and make my code more compact:

var result = from p1 in defaults
             join p2 in get on p1.PermissionName equals p2.PermissionName 
             into joined
             select new 
             {
                 PermissionName = p1.PermissionName, 
                 Permission = joined.Select(e => e.Permission == null ? false : true)
                                    .SingleOrDefault() 
             };

I could only go this far:

var result = defaults.Join(get, defaultKey => 
                          defaultKey.PermissionName, getKey => 
                          getKey.PermissionName, (a, b) => new 
                          { 
                              PermissionName = a.PermissionName, 
                              Permission = b.Permission 
                          });

As you can see that the Join() extension method does not provide a way to get the joined collection. I also searched online but could not find any leads. Please feel free to suggest.

1
  • 8
    You're using join ... into, which is represented with GroupJoin, not Join. Commented Apr 1, 2014 at 12:18

1 Answer 1

3

Use GroupJoin.

var result =
    defaults.GroupJoin(
        get,
        p1 => p1.PermissionName,
        p2 => p2.PermissionName,
        (p1, joined) =>
            new
            {
                PermissionName = p1.PermissionName,
                Permission = joined.Select(e => e.Permission != null)
                                   .SingleOrDefault()
            });
Sign up to request clarification or add additional context in comments.

Comments

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.