views:

51

answers:

1

I have a table example you can see below

ID Name         Value

3  NameOne      ValueOne    
7  NameTwo      ValueTwo    
10 NameThree    ValueThree

I need to select with Linq to Entity and get results as you can see in example below:

ItemID ItemName 

1      NameOne       
2      NameTwo         
3      NameThree
A: 

Try this:

using System.Linq;

var list = context.TableName.OrderBy(a => a.ID).Select(a => new { a.Name }).ToList().Select((a, index) => new { ItemID = index + 1, ItemName = a.Name } );

context.TableName.OrderBy(a => a.ID).Select(a => new { a.Name }).ToList() - this part takes name list

Select((a, index) => new { ItemID = index + 1, ItemName = a.Name } ); - this part adds index (ItemID)

LukLed
Thank you working great for me!
GodSmart