I am not sure I'm reading your question right, but I to me it looks like you want to group the source data on Username, and then group the Types in the source data and add count to the group.
If this is correct, then something like this might work for you;
using System;
using System.Linq;
namespace LinqStuff {
class BusinessObject {
public string Username { get; set; }
public string Type { get; set; }
}
class Program {
static void Main(string[] args) {
var data = new[] {
new BusinessObject { Username = "Alpha", Type="A" },
new BusinessObject { Username = "Alpha", Type="A" },
new BusinessObject { Username = "Bravo", Type="A" },
new BusinessObject { Username = "Charlie", Type="A" },
new BusinessObject { Username = "Delta", Type="A" },
new BusinessObject { Username = "Alpha", Type="B" },
new BusinessObject { Username = "Alpha", Type="C" },
new BusinessObject { Username = "Bravo", Type="C" },
new BusinessObject { Username = "Charlie", Type="C" },
new BusinessObject { Username = "Delta", Type="C" },
new BusinessObject { Username = "Bravo", Type="D" }
};
var groups = from entry in data
group entry by entry.Username
into g
select new {
Username = g.Key,
Types = from type in g group type by type.Type into t select new { Type = t.Key, Count = t.Count() }
};
foreach(var e in groups) {
Console.WriteLine("Username {0};", e.Username);
foreach(var t in e.Types) {
Console.WriteLine("\tType {0}, Count={1}", t.Type, t.Count);
}
}
}
}
}
If not, please elaborate on what your source data look like, and what you want the target grouping to look like and I'll adjust the answer.
Hope this helps,
Fredrik