[英]Retrieving data from Database and binding to List<> using EntityFramework MVC
I Have a table student in my database with some values and i want to display those values in webgrid using entityframework. 我的数据库中有一个Table Student,其中包含一些值,我想使用entityframework在webgrid中显示这些值。
I have done the samething before many times using Ado.net, but the purpose of this for me is just to learn entity framework and im absolute beginner to Entityframework. 在使用Ado.net之前,我已经做过很多次相同的事情,但是对我来说,这样做的目的仅仅是学习实体框架,并且是Entityframework的绝对初学者。
I used database first approach for this and in created a model Student with Id,FirstName,LastName,City. 我为此使用数据库优先方法,并用Id,FirstName,LastName,City创建了模型Student。
i also defined a List and i want to bind the result set from the database to this Student List and bind it to the webgrid 我还定义了一个列表,我想将数据库中的结果集绑定到此学生列表,并将其绑定到webgrid
public List<Student> stList;
public List<Student> GetStudents()
{
stList = new List<Student>();
EntityFWEntities OE = new EntityFWEntities();
var Res = OE.Students;
}
How can i assign the VAR value to List, also is this the correct approach i following or is there an other better approach, please correct me if im wrong 我如何将VAR值分配给列表,这也是我遵循的正确方法还是其他更好的方法,如果我错了,请更正我
To create the List<Student>
just use .ToList()
method. 要创建List<Student>
只需使用.ToList()
方法。
public List<Student> stList;
public List<Student> GetStudents()
{
using (EntityFWEntities OE = new EntityFWEntities())
{
//Assuming student has 4 properties ID, Name, Roll, DOB
stList = OE.Students.Select(s=> new MvcApplication4.Models.Student(){ ID=s.ID, Name=s.Name, Roll=s.Roll, DOB=s.DOB}).ToList();
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.