score:1
Accepted answer
var counter = 0;
var results =
from foo in bar
select new { foo.ID, foo.Name, Counter = ++counter };
score:2
With the overload of Select
that includes the index (also available for queryables) in fluent-syntax, you can do:
var results = bar.Select((foo, index) => new
{
foo.ID,
foo.Name,
Counter = index + 1
});
Note that this overload is not available in query-syntax.
score:0
Something like this maybe:
var result= db.bar.Select((foo,i)=>select new {foo.ID, foo.Name, Counter = i+1});
score:0
Use the indexed Select() overload:
var results = bar.Select((foo, i) => new { foo.ID, foo.Name, Counter = i + 1});
Source: stackoverflow.com
Related Articles
- Anonymous type with counter for each result
- get selected value from combobox with data source is anonymous type
- LINQ select query with Anonymous type and user Defined type
- This code returns distinct values. However, what I want is to return a strongly typed collection as opposed to an anonymous type
- How to assign a nullable int property in an anonymous type in LINQ with a Union?
- return list with anonymous type in entity framework
- C# LINQ build expression with anonymous type
- Anonymous Type with Linq and Guid
- LINQ query with GROUP BY and Count(*) into Anonymous Type
- LINQ - Contains with anonymous type
- Building an Expression with an anonymous type
- How to populate DataTable with anonymous LINQ result
- Call Generic Method with an anonymous Type (C#)
- Anonymous type in medium trust, works with Reflection not with Expressions
- LINQ select next record with each matching result
- When selecting an anonymous type with LINQ from EF, is there no way to run a method on an object as you select it?
- Linq - Using GroupBy with my own type vs anonymous type
- List with anonymous type
- How I can pass the LINQ result anonymous type to another method?
- The result type of the query is neither an EntityType nor a CollectionType with an entity element type
- Using SelectedItem property of ComboBox with Linq Anonymous Type
- Query expressions over source type 'dynamic' or with a join sequence of type 'dynamic' are not allowed
- Multi sums into one anonymous type with linq?
- LINQ Project properties into a new anonymous type with Contains
- How to create an anonymous type with calculated values
- Linq Groupby multiple columns with the same name (An anonymous type cannot have multiple properties with the same name)
- Getting single result from a query with anonymous types in Linq To Sql
- LINQ: Invalid anonymous type member declarator. Anonymous type members must be declared with a member assignment, simple name or member access
- LINQ deferred execution with a function's result as source (e.g. Console.ReadLine)
- How to create an anonymous type within linq query with TypeScript
- format date in linq query result
- linq to xml - get childrem of those nodes with a certain attribute
- Get the latest record and its details of every id - linq
- How to parametrize a query in Entity Framework?
- Comparing two lists using linq to sql
- Converting ORDER BY with multiple case statements
- LINQ single record forms or defaults if no record
- Wcf Rest Service Method Returns Errors Unable to Cast Object Of Type
- Need to Include() related entities but no option to do so
- trim away duplicates using Linq
- Placing a list of values into a viewmodel in ASP.Net MVC
- LINQ update statement using generic table and column names
- Clean way to write this query
- Entity Framework 6.1.1 DbJoinExpression get all left columns
- Collection list to grouped collection list using linq
- Entity framework with linq in asp.net mvc 5
- Finding comma separated Strings in List<String>
- Linq to Sql query with multiple aggregations
- How to use the let clause in C#
- Linq to SQL, is there support for a mid-string % wildcard?