How to add an order(autoIncrement) property to collection using Linq?
anonymous-types, c#, linq
Solution
If you are using linq to entities or linq to sql, get your data from the server and ToList() it. Most likely this answer will not translate to sql but I have not tried it.
List<string> myCollection = new List<string>();
myCollection.Add("hello");
myCollection.Add("world");
var result = myCollection.Select((s, i) => new { Identity = i, Value = s }).ToList();
Problem
Having: Initialize an anonymouse collection (I would send it as json) ``` var myCollection = new[] { new { Code = 0, Name = "", OtherAttribute = "" } }.ToList(); myCollection.Clear(); ``` And get the data. ``` myCollection = (from iPeople in ctx.Person join iAnotherTable in ctx.OtherTable on iPeople.Fk equals iAnotherTable.FK ... order by iPeople.Name ascending select new { Code = iPeople.Code, Name = iPeople.Name, OtherAttribute = iAnotherTable.OtherAtribute }).ToList(); ``` I want to add an Identity column, I need the collection ordered and a counted from 1 to collection.count. Is for binding this counter to a Column in a table (jtable). ``` var myCollection = new[] { new { Identity = 0, Code = 0, Name = "", OtherAttribute = "" } }.ToList(); myCollection = (from iPeople in ctx.Person join iAnotherTable in ctx.OtherTable on iPeople.Fk equals iAnotherTable.FK ... order by iPeople.Name ascending select new { Identity = Enum.Range(1 to n)//Here I don´t know how to do; in pl/sql would be rownum, but in Linq to SQL how? Code = iPeople.Code, Name = iPeople.Name, OtherAttribute = iAnotherTable.OtherAtribute }).ToList(); ```