Click here to Skip to main content
15,886,110 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Hi,

I don't know if what i want is possible with linq but i am trying to create a linq statement that goes through a list of a type with multiple fields and sort them. Each entry has "Username" and "Type" as fields i am looking to return a list of each Username containing a List of each Type with a count inside.

I have this so far
C#
var users = from p in results group p by p.UserName into g select new { Profile = g.Key, ProfileCount = g.Count() };


Is this possible? and how?

Thank you
Carl
Posted

1 solution

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
 
Share this answer
 
Comments
Carl Mailey 16-Sep-14 8:44am    
This is exactly what i was looking for, thank you very much
Fredrik Bornander 16-Sep-14 9:05am    
Sweet, glad I could help.
On a side note, you technically don't need the inner group as you could query the list of Types for .Count() to get the count.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900