Internal Connection Using Lambda (LINQ)

I am trying to write the following Linq (query style) in a 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, the extension method Join()does not provide a way to get the collection joined. I also searched the Internet but could not find any links. Please feel free to suggest.

+4
source share
1 answer

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()
            });
+2
source

Source: https://habr.com/ru/post/1534435/


All Articles