project to x with aggregates

Posts   
 
    
morten71
User
Posts: 80
Joined: 13-Jan-2009
# Posted on: 25-Feb-2011 11:34:21   

Sometimes I'd like to produce a subset of a fetched collection and apply various aggregates without having to re-visit the database:

Dim customers As New CustomerCollection customers.GetMulti(selectFilter)

Result CustomerName, CustomerType "customer 1", "customer type a", ... "customer 2", "customer type b", ... "customer 3", "customer type b", ... "customer 4", "customer type a", ... "customer 5", "customer type a", ... "customer 6", "customer type a", ...

from the customers collection I'd like to project to a datatable applying a count and a groupby ending up wth the structure:

CustomerType, CountOfCustomers "customer type a", "4" "customer type b", "2"

is this possible?

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 25-Feb-2011 21:39:33   

Are you using .Net3.5? If you do, you can use Linq2Objects to query the in-memory collection, like this:

var list = from c in customers
        group c by c.Country into g
        select new {g.Key, CustomerCount = g.Count()};

foreach (var e in list)
{
    Console.WriteLine("{0}\t{1}", e.Key, e.CustomerCount);
}
David Elizondo | LLBLGen Support Team
morten71
User
Posts: 80
Joined: 13-Jan-2009
# Posted on: 26-Feb-2011 17:33:34   

daelmo wrote:

Are you using .Net3.5? If you do, you can use Linq2Objects to query the in-memory collection, like this:

var list = from c in customers
        group c by c.Country into g
        select new {g.Key, CustomerCount = g.Count()};

foreach (var e in list)
{
    Console.WriteLine("{0}\t{1}", e.Key, e.CustomerCount);
}

Thanks for the hint. Linq to Objects is really elegant.