The let operator

Jun 4, 2014 at 8:40 AM
I'm not very experienced in LINQ so forgive me if I'm slow.
I have a LINQ query that looks like this
Region.Town =
                    (from house in Region.Town
                     let radiusSq = (from neighbor in Region.Town
                                     let distanceSq = Calc.DistanceSq(house.Position, neighbor.Position)
                                     orderby distanceSq
                                     select distanceSq).Skip(NeighborhoodSize).First()
                     orderby radiusSq descending
                     select house).Take(MaxTown).ToList();
As you can see there are let clauses. Does LINQ for C++ account for these?
Coordinator
Jun 11, 2014 at 7:38 PM
Hi.

Sorry for the late answer.

In LINQ for C# let is implemented in terms of Select (which cpplinq do support). Let's look how C# translates your LINQ expression above before compiling it:
Region.Town
    .Select (house => 
        new
        {
            house = house,
            // This is the let expression
            radiusSq = Region.Town.Select(neighbor => 
                new
                {
                    neighbor = neighbor,
                    distanceSq = Calc.DistanceSq(house.Position, neighbor.Position)
                })
                .OrderBy(v => v.distanceSq)
                .Select (v => v.distanceSq)
                .Skip (NeighborhoodSize)
                .First ()

        })
    .OrderBy (v => v.radiusSq)
    .Select (v => v.house)
    .Take (MaxTown)
    .ToList ()
    ;
cpplinq is perhaps a little bit misnamed because it doesn't support the C# LINQ syntax (ie what you wrote) but do support the functional way of working collections as popularized by LINQ for C#.

Hope this helped

Mårten