简体   繁体   English

添加一个新列以使用linq和实体列出?

[英]add a new column to list using linq and entity?

I want to add a new column 'Id' to my list which I'm retrieving the data from database using Linq and entity framework, i have 12 rows and i need to add a new column 'Id' which should start from 1 to 12 based on the number of rows I'm getting from database the id should be incremented starting from 1 我想在列表中添加新列“ Id”,我使用Linq和实体框架从数据库中检索数据,我有12行,我需要添加新列“ Id”,该列应从1到12根据我从数据库获取的行数,id应该从1开始递增

this is my query 这是我的查询

var Details = db.Details.Include(p => p.Name).Include(p => p.Gender);

the problem with this is that, I'm also implementing paging for my grid and when im clicking next page again the row numbers are being generated from sno 1, when my page 1 consists of records from row no 1 to 5, my page 2 row numbers should start from row no 6, but again my page 2 row numbers are starting from row no1.. 问题是,我也在为网格实现分页,当我再次单击下一页时,行号是从sno 1生成的,而我的页面1包含第1到5行的记录时,我的页面2行号应该从第6行开始,但是我的第2页行号还是从第1行开始。

im posting my flow of fetching the data and binding the data to grid 即时贴发布我的获取数据并将数据绑定到网格的流程

in the first step i have my model User.cs, which is interacting with database and fetching the data and storing the data in a List<> 第一步,我有我的模型User.cs,该模型与数据库交互并获取数据并将数据存储在List<>

in the next step, from my controller action method im invoking the method in my model User.cs which will fetch the data from the database and store it in the List<> 在下一步中,从我的控制器操作方法中调用模型User.cs中的方法,该方法将从数据库中获取数据并将其存储在List<>

In the 3rd step, from my view im giving the source property of my webgrid to the list in my model which is holding the data 在第三步中,从我的观点出发,我将Webgrid的source属性提供给保存数据的模型中的列表

I would sugges you use Skip and Take 我建议您使用“ SkipTake

public List<Details> GetDetails(int page, DbContext db)
{
   const int pageSize = 5;
   db.Details.Skip( (page*pageSize) ).Take(pageSize).ToList();
}

This would eliminate the need for a "index" column. 这将消除对“索引”列的需要。 It is a good thing because you can't add a column/property to a type, and still have the same type. 这是一件好事,因为您不能向类型添加列/属性,而仍然具有相同的类型。

eg if you want to return a List<Details> you have to add the property Id to the type Details . 例如,如果要返回List<Details> ,则必须将属性Id添加到Details类型。

For some documentation 对于一些文档

In case you want to do both paging and indexing the result. 如果您想同时分页和索引结果。 Try this: 尝试这个:

var Details = db.Details.Skip(page*pageSize)
                        .Take(pageSize)
                        .ToList()
                        .Select((p,i)=> new {Id = i,Detail = p});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM