LINQ: Selecting items from a list (Group By/Select/Sum & Max!)?

Var results = from kvp in source group kvp by kvp.Key.ToUpper() into g select new { Group = g, Max = g. Max(kvp => kvp. Value), Total = g.

Sum(kvp => kvp. Value) } into ag from x in ag. Group //SelectMany where x.

Value! = ag. Max //for the update to the question - note: possibly ambiguous let correct = ag.Group.

Where(y => y. Value == ag. Max).First().

Key select new { Key = x. Key, Max = ag. Max, Total = ag.

Total, Correct = correct } I kinda like the question because of all the little parts (some are rarely used) that are required to make the answer Max = g. Max(kvp => kvp. Value), Total = g.

Sum(kvp => kvp. Value) Performing multiple aggregations on a group is straightforward, yet challenging if you don't know how select a into be This clause takes everything that happened before and starts a new query with the target. Without it, I'd have to start a new query like this: var A = ... select a var B = from be in A It's important to note that the select into clause removes kvp and g from scope from be in source from a in b.

A //SelectMany This "unpacking" of the child collection turns my query about b's into a query about a's. Unlike the default Enumerable. SelectMany overload, it leaves the parent ( be ) in scope where x.

Value! = ag. Max Comparing a child's property with a parent's property?

Delightful.It's important to remember to break out where anytime you want to filter, even if you just grouped (there is no HAVING ).

Var results = from kvp in source group kvp by kvp.Key.ToUpper() into g select new { Group = g, Max = g. Max(kvp => kvp. Value), Total = g.

Sum(kvp => kvp. Value) } into ag from x in ag. Group //SelectMany where x.

Value! = ag. Max //for the update to the question - note: possibly ambiguous let correct = ag.Group.

Where(y => y. Value == ag. Max).First().

Key select new { Key = x. Key, Max = ag. Max, Total = ag.

Total, Correct = correct }; I kinda like the question because of all the little parts (some are rarely used) that are required to make the answer. Max = g. Max(kvp => kvp.

Value), Total = g. Sum(kvp => kvp. Value) Performing multiple aggregations on a group is straightforward, yet challenging if you don't know how.

Select a into be This clause takes everything that happened before and starts a new query with the target. Without it, I'd have to start a new query like this: var A = ... select a var B = from be in A It's important to note that the select into clause removes kvp and g from scope. From be in source from a in b.

A //SelectMany This "unpacking" of the child collection turns my query about b's into a query about a's. Unlike the default Enumerable. SelectMany overload, it leaves the parent (b) in scope.

Where x. Value! = ag.

Max Comparing a child's property with a parent's property? Delightful. It's important to remember to break out where anytime you want to filter, even if you just grouped (there is no HAVING).

Wowsers, quick answer & perfect. Thx! – Andrew White Jun 4 '10 at 1:21 Glad I could tickle the lesser used parts of LINQ.

I think the little addition makes it slightly more tricky? – Andrew White Jun 4 '10 at 1:39 Updated. The ambiguity comes from the possibility that multiple rows could have the same maximum value.

Also I want to mention that if all rows for a given key have the same value, then no rows are returned. – David B Jun 4 '10 at 2:47.

I have come up with two solutions, neither of which I like. I use a where clause to select the list of items (which I know will only be one), I can then check the count and make a Single call on this list if count is one, the other choice is to use a foreach and just break after getting the item. Neither of these seem like a good approach, is there a better way?

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions