简体   繁体   中英

convert this LINQ expression into Lambda

Guys, I have a hard time converting this below linq expression(left join implementation) to lambda expression (for learning).

var result = from g in grocery
       join f in fruit on g.fruitId equals f.fruitId into tempFruit
       join v in veggie on g.vegid equals v.vegid into tempVegg
       from joinedFruit in tempFruit.DefaultIfEmpty()
       from joinedVegg in tempVegg.DefaultIfEmpty()
       select new { g.fruitId, g.vegid, fname = ((joinedFruit == null) ? string.Empty :     joinedFruit.fname), vname = ((joinedVegg == null) ? string.Empty : joinedVegg.vname) };

Can some one suggest me how to do this.

And i really appreciate if someone give me the excellent tutorial links for "C# Lambdas & Linqs"

To convert a Linq query to it's Lambda equivalent:

  1. Download Linqpad and run your query.
  2. In the results window, click on the "λ" button in the toolbar. It's right above the Results window
  3. Your query will be converted to a Lambda expression equivalent!

在此处输入图片说明

Here's the heuristic that I follow:

Favor LINQ expressions over lambdas when you have joins.

I think that lambdas with joins look messy and are difficult to read.

I usually use ReSharper to help me convert things to method chains and lambda's, which helps me go back and forth fairly easy.

    var result = from g in grocery
                 join f in fruit on g.fruitId equals f.fruitId into tempFruit
                 join v in veggie on g.vegid equals v.vegid into tempVegg
                 from joinedFruit in tempFruit.DefaultIfEmpty()
                 from joinedVegg in tempVegg.DefaultIfEmpty()
                 select new { g.fruitId, g.vegid, fname = ((joinedFruit == null) ? string.Empty : joinedFruit.fname), vname = ((joinedVegg == null) ? string.Empty : joinedVegg.vname) };

And then using ReSharper's option of convert LINQ to method chain equals the following:

        var result =grocery .GroupJoin(fruit, g => g.fruitId, f => f.fruitId, (g, tempFruit) => new {g, tempFruit})
                            .GroupJoin(veggie, @t => @t.g.vegid, v => v.vegid, (@t, tempVegg) => new {@t, tempVegg})
                            .SelectMany(@t => @t.@t.tempFruit.DefaultIfEmpty(), (@t, joinedFruit) => new {@t, joinedFruit})
                            .SelectMany(@t => @t.@t.tempVegg.DefaultIfEmpty(),(@t, joinedVegg) =>
                                new
                                    {
                                        @t.@t.@t.g.fruitId,
                                        @t.@t.@t.g.vegid,
                                        fname = ((@t.joinedFruit == null) ? string.Empty : @t.joinedFruit.fname),
                                        vname = ((joinedVegg == null) ? string.Empty : joinedVegg.vname)
                                    });

Granted the output is less then desirable, but It at least helps in starting somewhere on understanding the syntax.

Here's how you might write this query in lambda:

var cus­tomers = new List {
new Cus­tomer { Com­pa­nyId = “AC”, Cus­tomerId = “Customer1” },
new Cus­tomer { Com­pa­nyId = “not-AC”, Cus­tomerId = “Customer2” },
};

var user­Cus­tomers = new List {
new User­Cus­tomer { Com­pa­nyId = “AC”, Cus­tomerId = “Customer1”, User = “not-admin” },
new User­Cus­tomer { Com­pa­nyId = “AC”, Cus­tomerId = “Customer1”, User = “admin” },
new User­Cus­tomer { Com­pa­nyId = “AC”, Cus­tomerId = “Customer2”, User = “not-admin” },
new User­Cus­tomer { Com­pa­nyId = “AC”, Cus­tomerId = “Customer2”, User = “admin” },
new User­Cus­tomer { Com­pa­nyId = “not-AC”, Cus­tomerId = “Customer1”, User = “not-admin”     },
new User­Cus­tomer { Com­pa­nyId = “not-AC”, Cus­tomerId = “Customer1”, User = “admin” },
new User­Cus­tomer { Com­pa­nyId = “not-AC”, Cus­tomerId = “Customer2”, User = “not-admin” },
new User­Cus­tomer { Com­pa­nyId = “not-AC”, Cus­tomerId = “Customer2”, User = “admin” }
};

Using query expression

var query =
from c in cus­tomers
join uc in user­Cus­tomers on
new { c.CompanyId, c.CustomerId } equals new { uc.CompanyId, uc.CustomerId }
where c.CompanyId == “AC” && uc.User == “admin“
select c;

Using lambda expressions

var lambda =  cus­tomers.Where(c => c.CompanyId == “AC”) // inner sequence
.Join(userCustomers.Where(uc => uc.User == “admin”), // outer sequence
c => new { c.CompanyId, c.CustomerId }, // inner key selec­tor
uc => new { uc.CompanyId, uc.CustomerId }, // outer key selec­tor
(c, uc) => c);

Both approach yields the same result (customer with company Id “AC” and customer Id “Customer1”), but as you can see, lambda expression is much harder to write and read!

Hope this helps!

Download LINQPad ; it comes with built-in samples for learning LINQ.

使用Reflector .NET :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM