开发者

Linq to entity select items and give id

开发者 https://www.devze.com 2022-12-29 23:16 出处:网络
I have a table example y开发者_运维知识库ou can see below ID NameValue 3NameOneValueOne 7NameTwoValueTwo

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)

0

精彩评论

暂无评论...
验证码 换一张
取 消