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 Articles
- 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()
- Difference between LINQ Lambda and SQL statement
- how to check given date exist in month or not?
- DefaultIfEmpty() Causing "System.NotSupportedException: LINQ to Entities does not recognize the method 'System.Collections.Generic.IEnumerable'"
- Linq query Join objects with different data sources?
- How to specify generic constraint: class inherited from interface?
- why doesn't Dictionary<TKey, TValue>.KeyCollection Class have its own Contains method?
- Find a set number of values from collection
- Merging objects in List<Object> on two properties
- DateTime to string conversion showing error
- Adding two lists having same structure C#
- Can I combine two Linq calls when they use different syntax?
- EntityFramework and Spatial Search POINT inside POLYGON
- Create a list of items within 'var response'
- Why LastOrDefault not working in asp.net linq?
- Declare variable for ConcurrentDictionary.Select()
- How To Handle Null In C#?
- Convert aggregate SQL to linq equivalent
- Like search for datetime fields using dynamic lambda expression
- Entity Framework, DbSet<T>, Dispose() performance questions
- Linq To Sql left join using multiple join clauses