Ricky
Ricky

Reputation: 35853

A LINQ question: map query expression to c# code

How do I translate the following query expression to corresponding C# code? Thanks.

        var list1 = (from ol in orderedList
                     from er in ol.Er
                     from rd in er.Rd
                     where rd.ftr != ""
                     select ol).ToList<CRInfo>();

Upvotes: 0

Views: 219

Answers (2)

Jon Skeet
Jon Skeet

Reputation: 1500815

It would translate to something like this:

var list1 = orderedList.SelectMany(ol => ol.Er, (ol, er) => new { ol, er })
                       .SelectMany(z => z.er.Rd, (z, rd) => new { z, rd })
                       .Where(z2 => z2.rd.frt != "")
                       .Select(z2 => z2.z.ol)
                       .ToList<CRInfo>();

The "z" and "z2" bits are transparent identifiers, used by the C# compiler to propagate multiple range variables through the query.

You may want to download LINQPad, which I believe lets you translate query expressions like this very easily.

Upvotes: 6

cdhowie
cdhowie

Reputation: 169028

Well, aside from the obvious fact that your code is already C# code...

I assume you want to obtain the actual Enumerable method calls? If so, you could just compile it and throw it into Reflector.

Upvotes: 1

Related Questions