Trying to LINQ it all together (Pt. I) - Epilogue

29.04.2007 - 19:22 (4 years, 9 months, 1 week ago)

programming, dotnet, TrivadisContent, LINQ

In the last post I said that I thought I'd have a bug in the keyword-based LINQ expression building. Of course, this is bollocks and not of the dog variety.

In my defense, the documentation I had read was getting it wrong, too, but other Microsoft online documentation gets it right. The following code shows the previous example written with the LINQ keywords.

      Person[] pees = Person.CreatePeopleList();
      var salarySums =
        from p in pees group p by p.Posn into groups
        select new { Position = groups.Key, TotalSalary = (from p in groups select p.Salary).Sum() };

Works like a treat. In retrospect it is quite clear why my attempt wouldn't work. I was starting an iteration on the elements of groups and then doing a select on those. A select, though, acts on every element of the passed in object. Those were, quite rightfully, things of the Person type.