Linq to entity select items and give id
I have a table example y开发者_运维知识库ou 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
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)
精彩评论