In my last article I discussed how to insert data in database using Entity Framework. I am taking the same example and file for this article. In this article I will show how we can select data from database using Entity Framework.
To show data on Form in aspx I am adding gridview on form and writing the selection code in page_load event.
protected void Page_Load(object sender, EventArgs e)
{
//object of entity class which holds all methods
techaltum_efEntities te = new techaltum_efEntities();
//selection from database using EF
IEnumerable<student> stu = te.students.ToList();
//bind this data to database
GridView1.DataSource = stu;
GridView1.DataBind();
}
Now execute this code and you will get following output:-
Figure 1
As above we show the all records. We can also filter these records using where clause as we used in LINQ.
For example I want to show the records whose name start with I. then we will select in the following way:-
//object of entity class which holds all methods
techaltum_efEntities te = new techaltum_efEntities();
//selection from database using EF
IEnumerable<student> stu = te.students.ToList().Where(x=>x.stu_name.StartsWith("i"));
//bind this data to database
GridView1.DataSource = stu;
GridView1.DataBind();
Now execute the code and you will get following output:-
Figure 3