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
}
}
Source: stackoverflow.com
Related Query
- LINQ Source Code Available
- creating Linq to sqlite dbml from DbLinq source code
- Linq count trouble
- source code for LINQ 101 samples
- Trouble with a LINQ 'filter' code throwing an error
- Trouble translating SQL query to LINQ with join and having count
- Trouble getting count with LINQ to XML
- c# Linq or code to extract groups from a single list of source data
- Convert string[] to int[] in one line of code using LINQ
- LINQ with groupby and count
- Code equivalent to the 'let' keyword in chained LINQ extension method calls
- LINQ - Left Join, Group By, and Count
- Linq with group by having count
- Linq code to select one item
- How to Count Duplicates in List with LINQ
- Using GroupBy, Count and Sum in LINQ Lambda Expressions
- Linq distinct - Count
- How are people unit testing code that uses Linq to SQL
- SELECT COUNT in LINQ to SQL C#
- LINQ performance Count vs Where and Count
- Linq - Grouping by date and selecting count
- How to count the number of elements that match a condition with LINQ
- LINQ Select Distinct Count in Lambda form
- How to count the number of code lines in a C# solution, without comments and empty lines, and other redundant stuff, etc?
- Get item count of a list<> using Linq
- PagedList using LINQ Skip and Take, but show paging using Count of results
- Multi-Group Column Count Linq
- Linq Count with Condition
- Is it possible to use Linq to get a total count of items in a list of lists?
- LINQ query to perform a projection, skipping or wrapping exceptions where source throws on IEnumerable.GetNext()
More Query from same tag
- Retrieving data LINQ vs Reflection
- Getting difference between two dates in days
- ASP.NET MVC 2 Architecture and LINQ data model
- Sum up column values in DataTable based on the value of another column
- Returning IEnumerable<string> from Linq query
- Linq orderby not working
- Accessing attributes on fields using extension method
- How to get only the date value from DateTime and count it?
- Discarding Intermediate results in Linq
- How to prepending a list before every row in another list?
- Entity Framework 5.0. What is wrong with my Query?
- Linq parameter error
- VS2015 does not see Linq in RazorEngine template
- Does ToLookup forces immediate execution of a sequence
- How to efficiently get unique combinations of a list of objects
- Locating a sub-object in MongoDB collection using LINQ/C#
- Get foreign key value without join In Entity-to-Linq
- Sometimes get Timeout expired exception with Linq to EF and after a restart of server, it works fine
- Refactor a LINQ JOIN out of the expression
- LINQ - Return data from list 2 occurrences
- LINQ many to many Left Join Grouping
- Error: 'StudentScore' is a type, which is not valid in the given context
- How to Except<> specifing another key? Or faster way to differences two huge List<>?
- How to extract an array from a List of anonymous objects
- Asp .Net LINQ to SQL Databinding, do a "lookup" without a lookup table?
- Entity Framework - Handle null value in Linq
- How to speed up this LINQ query with join?
- Calling fellow code nerds - Alternatives to Nested Loops?
- Simple update with Entity Framework
- Rearrange generic list after sequence update in c#