1

好吧,我从数据表中获得了这些数据:

Recom   House   49
Recom   Street  47
Recom   Floor   29
Area    House   5
Area    Floor   1
Observ  Floor   1

但我想要这样的东西:

Recom   House   49
Recom   Street  47
Recom   Floor   29
Area    House   5
**Area  Street  0**
Area    Floor   1
**Observ    House   0**
**Observ    Street  0**
Observ  Floor   1

我不能从存储过程中做到这一点,所以我正在尝试代码隐藏,但我真的不知道,我尝试了一些东西,但它不起作用,而且不是最好的解决方案。

int getTotal = ((from a in dtSerieDrill.AsEnumerable() 
                                             where a.Field<int>("idTipoRevision") == Int32.Parse(dtSerie.Tables[0].Rows[i]["idTipoRevision"].ToString())
                                             select a.Field<string>("TipoRevision")).Distinct()).ToArray().Length;

                            if (getTotal == arrayCat.Length) 
                            {
                                auxData += "{";
                                auxData += "name:'" + dtSerieDrill.Rows[n]["TipoRevision"].ToString() + "',";
                                auxData += "y:" + Double.Parse(dtSerieDrill.Rows[n]["Total"].ToString()) + "";
                                auxData += "},";
                            } else {
                                for (int g = 0; g < arrayCat.Length - 1; g++) 
                                {
arrayCat //categories(house,street,floor,etc)
                                    if (!arrayCat[g].Equals(dtSerieDrill.Rows[n]["Especialidad"].ToString()) && !(totalAux == (g+1))) 
                                    {
                                        auxData += "{";
                                        auxData += "name:'" + arrayCat[g] + "',";
                                        auxData += "y:0";
                                        auxData += "},";
                                        break;
                                    } else {
                                        auxData += "{";
                                        auxData += "name:'" + dtSerieDrill.Rows[n]["TipoRevision"].ToString() + "',";
                                        auxData += "y:" + Double.Parse(dtSerieDrill.Rows[n]["Total"].ToString()) + "";
                                        auxData += "},";
                                        break;
                                    }
                                }
                            }

我有那个代码,因为我会将它用于脚本。我可以添加带有空“总计”的行或创建一些

4

2 回答 2

1

我认为您可以使用数组或某种集合类型更轻松地实现此目标。

例如

//create you context
YourContext dbContext = new YourContext();

String[] firstCategory = dbContext.Categories.Where(x => x.categoryType == "YourFirstCategoryType").ToArray();
String[] secondCategory = dbContext.Categories.Where(x => x.categoryType == "YourSecondCategoryType").ToArray();

DataTable dt = new DataTable();
dt.Columns.Add("Category1");
dt.Columns.Add("Category2");
dt.Columns.Add("Total");

foreach(var c1 in firstCategory)
   foreach(var c2 in firstCategory)
   {
      //create a new row object 
      DataRow R = dt.NewRow();

      //set value to the colums
      R[0] = c1;
      R[1] = c2;
      R[2] = GetTotal(c1,c2);//GetTotal is some function that query database and returns total when you have c1 and c2 parameter, you should check is this combination doesn't exist you returns 0
      dt.Rows.Add(R);
   }

我希望它可以帮助你。

于 2013-04-12T14:55:11.350 回答
0

suppose you have a DataTable dt, you can add new rows by using dt.Rows.Add(parameters)

于 2013-04-11T23:53:17.293 回答