score:5
Accepted answer
enumerable.select
has an overload that projects the current index of the element in the sequence. also enumerable.where
and enumerable.skipwhile
/takewhile
have it. you can use it like a loop variable in a for
-loop which is sometimes handy.
one example which uses the index to create an anonymous type to group a long list into groups of 4:
var list = enumerable.range(1, 1000).tolist();
list<list<int>> groupsof4 = list
.select((num, index) => new { num, index })
.groupby(x => x.index / 4).select(g => g.select(x => x.num).tolist())
.tolist(); // 250 groups of 4
or one with where
which only selects even indices:
var evenindices = list.where((num, index) => index % 2 == 0);
it might also be important to mention that you can use these overloads that project the index only in method-syntax. linq query-syntax does not support it.
Source: stackoverflow.com
Related Query
- Where does "i" get its value in this LINQ statement?
- Where does this Linq statement get the selected value from?
- why does this linq code get exponentially slower when applying First() to projection?
- How to dynamically get a property value in which is in another property with Linq where statement
- LINQ XML Get value of element from multiple where statement
- Does this LINQ code perform multiple lookups on the original data?
- Using LINQ to get DataGridView row index where first column has specific value
- Expression to get LINQ with Contains to EF for SQL IN() where on entities child's property equals value
- How does this linq code that splits a sequence work?
- Where does this LINQ performance come from?
- Why does this LINQ query assign a value of 1 to a NULL value from the database?
- How do I get the first value from this collection using Linq to Entities?
- If you have a LINQ statement using the Select() method, is there a way to get a value from the next record?
- Get first value from table with where either have the value match or null with LINQ
- Get value from Database to Linq where clause
- Why the extension method of where for LINQ in this code would print out a single number while it shouldn't print anything at all?
- Get all the records where it does not exist in 2nd LINQ
- Why does this LINQ statement throw a timeout error?
- LINQ - get results where a certain property is in this other result set of the property's type
- providing a default value for where statement in linq that uses querystring
- loop through specific attribute using xml linq and c# and get its lowest value
- Linq - Get all keys from dictionary where all values in the value list are equal to criteria
- Get a cell value from datatable with two condition in where clause with LINQ
- How to add a where clause to this Entity Framework Linq statement
- Why does this LINQ left join statement compile down to an SQL statement with a sub-query in EF 6?
- How does this LINQ query get initial values for this string?
- Does Linq in Entity Framework code first use SQL or does it get the whole table first?
- linq where statement subject to a checkbox value
- How to find column value by comparing another column value in LINQ where case does not any matter
- Does Linq search the entire database when I run a select statement with a time restricting where clauses?
More Query from same tag
- Concatenating two lists of different types with LINQ
- Get descendants from specific XML element using LINQ to XML Query
- how to split json data and set related data to their related html text by ajax
- using LINQ convertAll when some conversions may be null
- Can't Get Count of More than one table fields Through Linq Query
- Build Dynamic Expression For Linq Where using PropertyInfo and a value
- oracle trunc(sysdate) to linq entityframework
- Strange collation issue using LINQ to SQL
- The cast to value type 'System.Int32' failed because the materialized value is null.
- Select with list among the variables
- Linq: how to select certain text "True" or "False" for corresponding boolean values?
- XML parsing to get a particular tag value in a list of XElement
- Convert new object creation to linq expression for linq to entities
- Get data part by part in asp.net Gridview
- LIKE query on DataTable using LINQ
- XML to LINQ with Digg API
- Find top-level parent in List with LINQ
- Get the difference between two lists using LINQ
- .NET MVC 3 Search in controller
- How do I add a percentage to a value in LINQ?
- Not sure how to handle this linq query
- Problem creating an Entity object/queryable from the result of a LINQ join in MVC
- Error on LINQ statement with enclosing variable
- In Linq to SQL, why does assigning a related entity create a ChangeSet insert?
- Linq way of checking that no items in a collection match any other item
- How to get ranged by index with LINQ allow to take previous or next items?
- Hashset Concatenate using Linq
- Dude, where's my object? or, Why does Linq not return my object?
- Different result in Linq with GroupBy and Count
- Query to filter contacts with given tags while having many to many relation between entities