score:3

Accepted answer

given that a badge only really has a badge id and a user, it sounds like you just need to get the badge id and the count of that badge for the use - which means just getting the key for the group:

var q = db.tblbadgeusers
    .where(c => c.userid == userid)
    .groupby(c => c.badgeid)
    .select(c => new { badgecount = c.count(), badgeid = c.key });

if there's more information on each badge, you might want to do:

var q = db.tblbadgeusers
    .where(c => c.userid == userid)
    .groupby(c => c.badgeid)
    .select(c => new { badgecount = c.count(), badgeid = c.key, badges = c });

then you could do:

foreach (var badgetype in q)
{
    console.writeline("{0}: {1}", badgetype.badgeid, badgetype.badgecount);
    foreach (var badge in q.badges)
    {
        // deal with the badge information
    }
}

Related Query

More Query from same tag