score:2
Accepted answer
Assuming the OwnerNames will be identical for all the items in the group then you could do something like:
var linqQuery2 = (from f in linqQuery.ToList()
group f by f.OwnerId into myGroup
select new
{
OwnerName = myGroup.First().OwnerName,
OrderCount = myGroup.Count()
});
Edit:
If you want to use the first item multiple times, I'd suggest you use the 'let' operator:
var linqQuery2 = (from f in linqQuery.ToList()
group f by f.OwnerId into myGroup
let first = myGroup.First()
select new
{
OwnerName = first.OwnerName,
OrderCount = myGroup.Count()
});
score:1
Try
var linqQuery2 = (from f in linqQuery.ToList()
group f by f.OwnerName
into myGroup
select new
{
OwnerName = myGroup.Key,
OwnerCount = myGroup.Count()
});
Alternately:
var linqQuery2 = linqQuery.ToList()
.GroupBy(f => f.OwnerName)
.Select(myGroup => new
{
OwnerName = myGroup.Key,
OwnerCount = myGroup.Count()
});
Source: stackoverflow.com
Related Articles
- LINQ group by query using reflected property name
- Filling in missing dates using a linq group by date query
- Linq query using group by and having
- How can I write the following code more elegantly using LINQ query syntax?
- Linq sub query when using a repository pattern with EF code first
- Using LINQ query result for data source for GridControl c#
- LINQ Query using Group By
- LINQ query in C# using group and join
- Using Group in LINQ Query
- How can I check the number of calls to the database in LINQ query when using .NET Core and Code First?
- How do I convert this tSQL statement to LINQ using group by in a sub query
- Group by year and month with given date range using Lambda or LINQ Query
- How to Select top (5) contributors group by Business type code in c# linq query
- Linq query for two tables using SUM and Group By
- Convert Sql Query To linq using sum with group by
- LINQ with sub select query using sum and group by?
- Linq query to get data from Database using group by clause
- Simple linq query using group in order to return a one to many relationship
- Write LINQ query without select or group clause using extension method syntax
- Using FromSqlRaw or Linq query to Group and Sum data
- How could I query average and group by using LINQ in ASP.NET C#
- LINQ-to-SQL: Select mulitple properties from top row of each GROUP using LINQ query
- How to dynamically group a list and select values using linq query in C#?
- LINQ Intersect Query for Many to Many relationship using .NET4 MVC4 Code First
- What's the best way to construct this LINQ query using orderby and group by?
- Errors using DefaultIfEmpty() in LINQ query with group by
- how to fetch data from database using linq query for relationship 1:N and N:N (between 3 entity) in asp.net mvc EF code first?
- Linq query using Group by and Joins
- not getting output in group by using linq query
- LINQ query using Group By and Let
- How to neatly query corresponding object array items?
- Convert a sequence of arrays T myArray[] to IEnumerable<T> in c#
- How to get IDictionaryEnumerator from generic IDictionary?
- C# linq anonymous include
- Check if single() LINQ return NULL
- lambda expression with aggregation function
- .Except<T> is throwing an Exception
- Where should I put this query to avoid having to run it multiple times?
- linq to sql, select by dates that match to month and year from dates array
- Replace XML-element with LINQ to XML
- Best way to get all words like @*@ from a string in C#
- How to convert the following foreach loop to linq format?
- How to efficiently duplicate a specific value in a list
- Value is in enum list
- Getting Count() for basic IQueryable instance
- Array.Contains runs very slow, anyone shed some light?
- Replacing loops with linq code
- Entity Framework Core - (Kind of subquery)
- Compare strings with non-English characters?
- MongoDB .NET driver and text search