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 Query
- 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
More Query from same tag
- Expressions: Variable referenced from scope, but it is not defined exception
- Is it possible to access the generated SQL with Linq To Entities
- Comparing two dates in LINQ to SQL
- Increase linq query effieciency
- How could I accomplish this in Linq?
- Property for BindingSource to return alphabetically sorted list
- Performing a wildcard search
- Why can ToList modify original value without reassigning to itself?
- How to return a list of elements that were used in Average as well as their Average
- How do I delete all child elements using linq to sql?
- Order by date in different directions
- union of unknown set of xmls
- asp.net - Specified cast not valid Linq
- Linq with union on 3 tables
- Linq - Orderby not ordering
- Operator '==' cannot be applied to operands of type 'KeyValuePair' and 'string'?
- Change the Name of an array into Object with JavaScript
- ASP.Net IEnumerable List: store values to chart
- What is the best way to return a projected query to add filter afterward
- How to Join two Entities and Get Unique Rows from One
- How would I perform a left join with coalesce in Linq?
- How to pull one column from second table in Linq query with join
- How to define default values with Entity Framework C#?
- Split DataTable Values(Comma Separated Values) Into multiple rows
- C# LINQ statement with joins, group by and having then mapped into list object
- Filter Child Entities in Controller LINQ
- Merge multiple columns in a single object C#
- How to split a Guid List in to small part of lists
- Dealing with nulls indynamically created linq expression
- LINQ: OrderByDescending by last value of multiple dates within the table