【问题标题】:Manage query with dynamic data(commodity)使用动态数据管理查询(商品)
【发布时间】:2015-03-24 05:47:52
【问题描述】:

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

数据库表是:

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();

    }
}

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

【问题讨论】:

  • 第一个商品数量,使用 rows.count 或 count(sql),只需编写一个循环 for(i=0;i
  • 如何在 linq 查询中使用 for 循环?

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


【解决方案1】:

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

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

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);
                    });

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-07-16
    • 2018-08-27
    • 1970-01-01
    • 1970-01-01
    • 2012-06-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多