Now, I'm not saying this is actually a good approach; it's probably going to be slow and the right way to do this, if performance matters, may be to sort the whole collection by these different criteria and then look at the different parts of the sorted collection But if you want to use GroupBy and IGroupings to manage it, you're working at it from the wrong end. You want to start at the deepest level first and work up var groups = rows . GroupBy(x => new { x.
A, x. B, x. C, x.
D, x. E, x. F }) .
GroupBy(x => new { x.Key. A, x.Key. B, x.Key.
C, x.Key. D, x.Key. E }) .
GroupBy(x => new { x.Key. A, x.Key. B, x.Key.
C, x.Key. D, }) . GroupBy(x => new { x.Key.
A, x.Key. B, x.Key. C }) .
GroupBy(x => new { x.Key. A, x.Key. B }) .
GroupBy(x => x.Key. A); groups.First(). Key; // will be an A value groups.First().First().First(); // will be an A, B, C group.
Now, I'm not saying this is actually a good approach; it's probably going to be slow and the right way to do this, if performance matters, may be to sort the whole collection by these different criteria and then look at the different parts of the sorted collection. But if you want to use GroupBy and IGroupings to manage it, you're working at it from the wrong end. You want to start at the deepest level first and work up.
Var groups = rows . GroupBy(x => new { x. A, x.
B, x. C, x. D, x.
E, x. F }) . GroupBy(x => new { x.Key.
A, x.Key. B, x.Key. C, x.Key.
D, x.Key. E }) . GroupBy(x => new { x.Key.
A, x.Key. B, x.Key. C, x.Key.
D, }) . GroupBy(x => new { x.Key. A, x.Key.
B, x.Key. C }) . GroupBy(x => new { x.Key.
A, x.Key. B }) . GroupBy(x => x.Key.
A); groups.First(). Key; // will be an A value groups.First().First().First(); // will be an A, B, C group.
This is very close to what i'm looking for, and really not that slow. I can probably make this work, but it would be nice if I could make the subgroups only a single column, not multiples. – Mystere Man Aug 8 '10 at 18:14 Yeah, it looks like the multiple fields are necessary to get the lower groups to work.
You da man! Unless you're a woman, in which case you da woman! – Mystere Man Aug 8 '10 at 18:24 As a bonus, If I convert my Linq-to-Sql query to a list prior to doing the grouping, then it significantly faster, because it does it in memory rather than with multiple queries on the sql server.
Adds a little load to the Web server, but I think it's a net win. – Mystere Man Aug 9 '10 at 2:38.
GroupBy actually supports giving a list of elements to group by. Each group will contain the same first 3 items (A, B & C). You can get the key with the .
Key method, and play around with the different rows with foreach. See Example: var groups = Elements. GroupBy(x => new {x.
A, x. B, x. C}); foreach (var group in groups) { Trace.
WriteLine(group. Key + ": " + group.Count()); foreach (var row in group) { Trace. WriteLine(row.
D); } } Edit: Ahh, ok - what you need is this then: var groups = Elements . GroupBy(a => new {a. A}) .
Select(g1 => new { A = g1. Key, Groups = g1 . GroupBy(b=> new {b.
B}) . Select(g2 => new { B = g2. Key, Groups = g2 .
GroupBy(c => new {c. C}) . Select(g3 => new { C = g3.
Key, Rows = g3 }) }) }); foreach (var groupA in groups) { Trace. WriteLine(groupA. A); foreach (var groupB in groupA.
Groups) { Trace. WriteLine("\t" + groupB. B); foreach (var groupC in groupB.
Groups) { Trace. WriteLine("\t\t" + groupC. C); foreach (var row in groupC.
Rows) { Trace. WriteLine("Row: " + row.ToString()); } } } }.
No, that creates one group, with a, b, and c. Not collections of each. – Mystere Man Aug 8 '10 at 18:07 Ok - I have changed it to nest the groups for you - better than the other answer since you don't need to do all the excessive nesting / multiple fields etc – Shaun McCarthy Aug 8 '10 at 18:37 Yes, that was actually kind of where I was trying to go originally, and I now see what I did wrong.
However, mquander's response is really more of what I meant to do rather than what I was doing. The reason is that all the ojbects in the hiearchy are IGroupings rather than a mix of groupsings and IOrderedQueryables which is what your solution does. But i'll +1 you – Mystere Man Aug 9 '10 at 2:23.
I am using GroupBy create a hierarchical set of groups to use in multiple child grids. Assume I have a query with with 6 columns, a, b, c, d, e, f. Now, I need to group by a, then by b, then by c.
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.