使用动态数据管理查询(商品)

时间:2015-03-24 05:47:52

标签: c# asp.net gridview entity-framework-5

我在数据库中有一个包含动态记录的表。我使用静态数据(商品)获得了正确的结果,但我需要动态数据的结果。

数据库表是:

Id  Month  Commodity  Amount
----------------------------
1   May     wheat      100
2   May     rice       200
3   June    wheat      400
4   July    maize      100
5   June    wheat      100

我的结果:

 Month      wheat    rice    maize
 --------------------------------
 May        100      200     
 June       500
 July                        100    

我的aspx代码:

<asp:GridView ID="grdData" runat="server">
</asp:GridView>

和aspx.cs代码

protected void Page_Load(object sender, EventArgs e)
{
    if (!Page.IsPostBack)
    {
        getdata();
    }
}

public void getdata()
{
    using (GridFormatEntities context = new GridFormatEntities())
    {
        var result = (from r in context.tbl_Commodity
                          select new
                          {
                              Id = r.Id,
                              Month = r.Month,
                              Commodity = r.Commodity,
                              Amount = r.Amount
                          })
             .GroupBy(r => r.Month)
             .Select(r => new
             {
                 Month = r.Key,

                 Wheat = r.Where(x => x.Commodity == "Wheat").Sum(x => x.Amount),
                 Rice = r.Where(x => x.Commodity == "Rice").Sum(x => x.Amount),
                 maize= r.Where(x => x.Commodity == "maize").Sum(x => x.Amount),

             }).ToList();
        grdData.DataSource = result;
        grdData.DataBind();

    }
}

在上面的查询中它是静态的(小麦,大米和玉米),但我动态地需要这些商品...请帮助我如何管理动态数据(商品)。

1 个答案:

答案 0 :(得分:0)

如果您可以使用数据表而不是列表,则以下代码可以帮助您。请记住它只是一个示例代码,可以帮助您理解这个概念。请根据您的输入数据检查循环效率。

它填充了表,您可以将其分配给数据源。

var list = new[]
                           {
                               new { Id = 1, Month = 2, Commodity = "Wheat", Amount = 20 },
                               new { Id = 2, Month = 2, Commodity = "Maize", Amount = 30 },
                               new { Id = 3, Month = 2, Commodity = "Barley", Amount = 30 },
                               new { Id = 4, Month = 1, Commodity = "Wheat", Amount = 20 },
                               new { Id = 5, Month = 1, Commodity = "Maize", Amount = 30 },
                               new { Id = 6, Month = 3, Commodity = "Barley", Amount = 30 }
                           };

            // group data by month and keep all rows
            var data =
                list.GroupBy(r => r.Month)
                    .Select(
                        r =>
                        new 
                            {
                                Month = r.Key,
                                Data = r.ToList()
                            })
                    .ToList();

            // get a list of all available commodities
            var allColumns = data.SelectMany(d => d.Data.Select(s => s.Commodity)).Distinct().ToList();

            // create a table
            var datatable = new DataTable();
            datatable.Columns.Add(new DataColumn("Month"));

            // add all commodities as columns
            allColumns.ForEach(c=>datatable.Columns.Add(new DataColumn(c)));

            // create 1 row for each month group and fill the columns
            data.ForEach(
                d =>
                    {
                        var row = datatable.NewRow();
                        row["Month"] = d.Month;
                        allColumns.ForEach(c => row[c] = d.Data.Where(g => g.Commodity == c).Sum(g => g.Amount));
                        datatable.Rows.Add(row);
                    });