c# - How can I insert a sequential number into a field using LINQ? -


i have following code using put data relational table using entity framework:

    public ilist<objective> createobjectives()     {         var objectivenames = new[]         {             "objective 1",             "objective 2",             "objective 3",             "objective 4",             "objective 5",             "objective 6",             "objective 7",             "objective 8"         };         var objectives = objectivenames.select(o => new objective         {             objectiveseq = ??,             name = o,             description = o + " description",             modifieddate = datetime.now         }         );         return objectives.tolist();     } 

i have new field in table name objectiveseq. how can modify linq insert sequential number in field starting 1.

there's overload select function. can find here.

enumerable.select<tsource, tresult> method (ienumerable<tsource>, func<tsource, int32, tresult>) 

take @ sample shown in page.

 string[] fruits = { "apple", "banana", "mango", "orange", "passionfruit", "grape" };   var query = fruits.select((fruit, index) => new { index, str = fruit });   foreach (var obj in query)  {     console.writeline("{0}", obj);  } 

you can see using (fruit, index). selecting both element , index.

the output

{ index = 0, str = apple } { index = 1, str = banana } { index = 2, str = mango } { index = 3, str = orange } { index = 4, str = passionfruit } { index = 5, str = grape } 

Comments