score:9
Accepted answer
this depends on your exact needs, but you can use:
var grouparray = groups.toarray();
similarly, you can use tolist
. these data structures are sequential, and each group has an index.
if you do need the index on the object you create, another option is to use select
:
list.groupby(c => c.name)
.select((details, ind) =>
new
{
name = details.key,
detailrecords = details,
index = ind
});
score:1
if it's just a sequential group number, just use the count() method on your ienumerable.
var groups =
from c in list
group c by c.name into details
select new {name = details.key, detailrecords = details};
for(int i = 0; i < groups.count(); i++)
{
//process records
}
then, if you need the specific group number, you can just grab i
.
score:6
this should do the trick:
int groupnumber = 0;
var groups =
from c in list
group c by c.name into details
select new { name = details.key, detailrecords = details, grpnum = groupnumber++};
Source: stackoverflow.com
Related Query
- Group Number in a LINQ Group by Query
- How can I check the number of calls to the database in LINQ query when using .NET Core and Code First?
- How to Select top (5) contributors group by Business type code in c# linq query
- How to group and return Column1 with total number of "true" in boolean Column2 Linq query and display in View?
- Linq Query Group By and Selecting First Items
- How To Project a Line Number Into Linq Query Results
- C# LINQ Query - Group By
- How to use Linq to group every N number of rows
- Linq group by query
- LINQ query to perform a projection, skipping or wrapping exceptions where source throws on IEnumerable.GetNext()
- Entity Framework - Linq query with order by and group by
- Linq query to sum by group
- LINQ Query with GROUP and SUM
- Simple sql to Linq query with group by and aggregate functions
- Linq to Entities group by week number
- Translating query with GROUP BY and COUNT to Linq
- Convert string to decimal in group join linq query
- C# get minute number from date in linq group by
- LINQ group by query using reflected property name
- Flattening Linq Group query
- Filling in missing dates using a linq group by date query
- LINQ query with GROUP BY and Count(*) into Anonymous Type
- Linq query using group by and having
- Is there any way to create a LINQ query as a variable without having the data source (yet)?
- linq query group by in a list of strings
- How can I create a dynamic LINQ query in C# with possible multiple group by clauses?
- LINQ Source Code Available
- how do i use group by only on date from datetime in LINQ query
- Linq query Group By with contains
- Linq Query to Group By Multiple Columns But Get Additional Properties
More Query from same tag
- load one item per page [ Pagination ]
- Conditional order by in linq
- How to Check if Data Exists in Table using LinQ to SQL
- LINQ Left Outer Join w/ Date Range restriction on Right table?
- Linq to SQL how to do “where [column] in (list of values)”
- null reference exception on using DefaultIfEmpty()
- LINQ, Group by property while keeping other property sort together
- How to iterate over grouped LINQ data in MVC view
- Need to compare 2 generic list of same type based on dynamic columns/fields that will be know at run time
- Trying to convert cross join transact-sql into LINQ
- Linq statement returns null
- linq: order by random
- A Better DataTable
- There is Already an open DataReader that must be closed first
- linq filtering by class type
- How to select a property from an anonymous type
- Input string was not in correct format error in LINQ to Excel query
- Subquery with linq
- Search Query in Linq with EF
- Generate LINQ query from multiple controls
- Find items in one list based on items in another list of a different type
- Unexpected Result LINQ Where Clause
- Is it possible to express this code in LINQ?
- How to linq join if datatable doesn't have all the rows
- Concert tabular data into Objects through LINQ
- Advantage of Func<T> in c#?
- LINQ/Lambda expression: Join lists and find the average counts of data with the given formula
- Filtering XElements by descendent element's attributes
- DataPager not working with my datasource
- Query for List within List