tags:

views:

1124

answers:

5

Hi

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"

Cheers

Ramesh Vel

+4  A: 

You can take a look at 101 LINQ Samples and C# 3.0 QUERY EXPRESSION TRANSLATION CHEAT SHEET

Dzmitry Huba
thanks for the links Dzmitry... the cheat sheet is useful... i already have a look at 101 samples..
Ramesh Vel
hey where would i get the source code for the 101 samples.. any idea.. i couldnt find there...
Ramesh Vel
+1  A: 

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

Mitch Wheat
i thought that was only the paid version that had loads of samples etc
Andi
The free version comes with samples too.
Mitch Wheat
+2  A: 

Use Reflector .NET :)

leppie
+1  A: 

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.

Jim G.
thanks jim for the reply. But i wanted to do this in lambda to get familiar with.. i mentioned that already its for learning...
Ramesh Vel
+1  A: 

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 => @[email protected](), (@t, joinedFruit) => new {@t, joinedFruit})
                            .SelectMany(@t => @[email protected](),(@t, joinedVegg) =>
                                new
                                    {
                                        @t.@[email protected],
                                        @t.@[email protected],
                                        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.

Mark
thanks Mark for your response.. i will run it over here and will let you know if that works..
Ramesh Vel