如何在数据表中存储dbcontext.emps.ToList()数据

时间:2017-06-21 17:40:10

标签: entity-framework linq c#-3.0

如何存储dbcontext.emp.tolist();数据表中的数据使用实体框架数据库第一种方法和linq执行排序。 在这里我上传我的代码。 请帮忙。 如何使用实体框架和linq执行网格的投票操作。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
     using System.Data;

    namespace sortingby_crud
   {
public partial class WebForm2 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!this.IsPostBack)
        {
            //this.BindGrid();
            GridView1.DataSource = BindGrid();
            GridView1.DataBind();
        }

    }
    private DataTable BindGrid()
    {
        using (dbEntities db = new dbEntities())
        {
            //GridView1.DataSource = db.emps.ToList();
            //GridView1.DataBind();
            DataTable dt = new DataTable();
          dt=  db.emps.ToList();
            return dt;
        }
    }
    public SortDirection dir
    {
        get
        {
            if (ViewState["dirState"] == null)
            {
                ViewState["dirState"] = SortDirection.Ascending;
            }
            return (SortDirection)ViewState["dirState"];
        }
        set
        {
            ViewState["dirState"] = value;
        }
    }

    protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
    {
        string sortingDirection = string.Empty;
        if (dir == SortDirection.Ascending)
        {
            dir = SortDirection.Descending;
            sortingDirection = "Desc";
        }
        else
        {
            dir = SortDirection.Ascending;
            sortingDirection = "Asc";

        }
        DataView sortedView = new DataView(BindGrid());

        sortedView.Sort = e.SortExpression + " " + sortingDirection;

        GridView1.DataSource = sortedView;

        GridView1.DataBind();
    }

}

}

1 个答案:

答案 0 :(得分:0)

          public DataTable data()      // or  keep return type is also  
                    as void . 
                 /convert db.context in data table.
         {
        DataTable table = new DataTable();

        var list = db.SupplierAssignmentones.ToList();
        //Defining the header for the datatable.
        table.Columns.Add("VendorId", typeof(int));
        table.Columns.Add("VendorName", typeof(string));
        table.Columns.Add("VendorDescription", typeof(string));
        table.Columns.Add("SupHie1_CD", typeof(string));
        table.Columns.Add("SupHie2_CD", typeof(string));
        table.Columns.Add("SupHie3_CD", typeof(string));
        table.Columns.Add("Is_Active", typeof(string));

        //assigning the data for data table rows.

        foreach (var data in list)
        {
            table.Rows.Add(data.VendorId, data.VendorName, data.VendorDescription, data.SupHie1_CD, data.SupHie2_CD, data.SupHie3_CD, data.Is_Active);
        }
        return table;
    }