我正在从我的数据库中检索一个DataSet,但我想以不同的方式呈现它,然后再返回它。我目前的行看起来像第一个表,我在gridview中显示它们。
我现在想要重构数据,以便输出类似于第二个表。我并不总是得到相同的“周”,所以我必须检查我有几个星期并重复它们来重建数据。我通过“var result”执行此操作但我不知道如何正确使用返回周数的结果来构建我的gridview。
|------------------------------------------|
| Ansvarig | Name | Week | Tid |
|------------------------------------------|
| John Doe | John Andersson | 4 | 29,0 |
| John Doe | John Andersson | 5 | 0,0 |
| John Doe | John Andersson | 5 | 0,0 |
| John Doe | John Andersson | 13 | 8,0 |
| John Doe | Anders Cameron | 4 | 8,0 |
| John Doe | Anders Cameron | 12 | 11,0 |
| John Doe | Steve Smith | 4 | 8,0 |
| John Doe | Steve Smith | 6 | 0,0 |
| John Doe | Steve Smith | 6 | 0,0 |
| John Doe | Steve Smith | 7 | 0,0 |
|-----------------------------------------------------------------------------------------|
| Ansvarig | Name | Week 4 | Week 5 | Week 6 | Week 7 | Week 12 | Week 13 |
|-----------------------------------------------------------------------------------------|
| John Doe | John Andersson | 29,0 | 0,0 | 0,0 | 0,0 | | 8,0 |
| John Doe | John Andersson | 29,0 | 0,0 | 0,0 | 0,0 | | 8,0 |
| John Doe | Anders Cameron | 8,0 | | | | 11,0 | |
| John Doe | Steve Smith | 8,0 | | 0,0 | 0,0 | | |
| John Doe | Steve Smith | 8,0 | | 0,0 | 0,0 | | |
protected void update_Click1(object sender, EventArgs e)
{
TimeView view = new TimeView();
DataSet ds = view.TimeOverview(fromDatum, tomDatum, ansvarig, anvandare);
GridView1.DataSource = ds;
GridView1.DataBind();
DataView dv = new DataView(ds.Tables[0]);
dv.Sort = "Vecka";
var result = from row in ds.Tables[0].AsEnumerable()
where 1 == 1
group row by new { weeknumber = row["Week"] } into g
select new
{
weekMax = g.Max(x => x["Week"]),
};
}
答案 0 :(得分:2)
您需要分组和转动以获得结果。
DataTable
来存储最终结果DataTable
或对象列表解决方案如下所示:
using System;
using System.Data;
using System.Xml;
using System.Data.Linq;
using System.Data.DataSetExtensions;
using System.Linq;
public class Program
{
public void Main()
{
var results = GetResults(GetTestData());
foreach(DataColumn dc in results.Columns)
{
Console.Write("{0},", dc.ColumnName);
}
Console.WriteLine();
foreach(DataRow dr in results.Rows)
{
foreach(DataColumn dc in results.Columns)
{
Console.Write("{0},", dr[dc.ColumnName]);
}
Console.WriteLine();
}
}
private DataTable GetResults(DataSet ds)
{
var result = (from row in ds.Tables[0].AsEnumerable()
let ansvarig = row.Field<string>("Ansvarig")
let name = row.Field<string>("Name")
let week = row.Field<int>("Week")
let tid = row.Field<double>("Tid")
group row by new { ansvarig, name, week } into grp
select new
{
Ansvarig = grp.Key.ansvarig,
Name = grp.Key.name,
Week = grp.Key.week,
Total = grp.Sum(r => r.Field<double>("Tid"))
}).ToList();
var uniqueWeeks = result
.Select(item => new { Week = item.Week })
.OrderBy(x => x.Week)
.Distinct()
.ToList();
var dt = new DataTable();
dt.Columns.Add(new DataColumn("Ansvarig", typeof(System.String)));
dt.Columns.Add(new DataColumn("Name", typeof(System.String)));
// add week columns
foreach(var item in uniqueWeeks)
{
Console.WriteLine("Week: {0}", item.Week);
dt.Columns.Add(new DataColumn(string.Format("Week {0}", item.Week), typeof(System.String)));
}
// add rows
foreach (var item in result)
{
var foundRow = dt.AsEnumerable().FirstOrDefault(r => r.Field<string>("Ansvarig") == item.Ansvarig && r.Field<string>("Name") == item.Name);
if (foundRow == null)
{
var dr = dt.NewRow();
dr["Ansvarig"] = item.Ansvarig;
dr["Name"] = item.Name;
dr[string.Format("Week {0}", item.Week)] = item.Total;
dt.Rows.Add(dr);
}
else
{
foundRow[string.Format("Week {0}", item.Week)] = item.Total;
}
}
return dt;
}
private DataSet GetTestData()
{
var ds = new DataSet();
var dt = new DataTable();
dt.Columns.Add(new DataColumn("Ansvarig", typeof(System.String)));
dt.Columns.Add(new DataColumn("Name", typeof(System.String)));
dt.Columns.Add(new DataColumn("Week", typeof(System.Int32)));
dt.Columns.Add(new DataColumn("Tid", typeof(System.Double)));
var dr = dt.NewRow();
dr["Ansvarig"] = "John Doe";
dr["Name"] = "John Andersson";
dr["Week"] = 4;
dr["Tid"] = 29;
dt.Rows.Add(dr);
dr = dt.NewRow();
dr["Ansvarig"] = "John Doe";
dr["Name"] = "John Andersson";
dr["Week"] = 5;
dr["Tid"] = 0;
dt.Rows.Add(dr);
dr = dt.NewRow();
dr["Ansvarig"] = "John Doe";
dr["Name"] = "John Andersson";
dr["Week"] = 5;
dr["Tid"] = 0;
dt.Rows.Add(dr);
dr = dt.NewRow();
dr["Ansvarig"] = "John Doe";
dr["Name"] = "John Andersson";
dr["Week"] = 13;
dr["Tid"] = 8;
dt.Rows.Add(dr);
dr = dt.NewRow();
dr["Ansvarig"] = "John Doe";
dr["Name"] = "Anders Cameron";
dr["Week"] = 4;
dr["Tid"] = 8;
dt.Rows.Add(dr);
dr = dt.NewRow();
dr["Ansvarig"] = "John Doe";
dr["Name"] = "Anders Cameron";
dr["Week"] = 12;
dr["Tid"] = 11;
dt.Rows.Add(dr);
dr = dt.NewRow();
dr["Ansvarig"] = "John Doe";
dr["Name"] = "Steve Smith";
dr["Week"] = 4;
dr["Tid"] = 8;
dt.Rows.Add(dr);
dr = dt.NewRow();
dr["Ansvarig"] = "John Doe";
dr["Name"] = "Steve Smith";
dr["Week"] = 6;
dr["Tid"] = 0;
dt.Rows.Add(dr);
dr = dt.NewRow();
dr["Ansvarig"] = "John Doe";
dr["Name"] = "Steve Smith";
dr["Week"] = 6;
dr["Tid"] = 0;
dt.Rows.Add(dr);
dr = dt.NewRow();
dr["Ansvarig"] = "John Doe";
dr["Name"] = "Steve Smith";
dr["Week"] = 7;
dr["Tid"] = 0;
dt.Rows.Add(dr);
ds.Tables.Add(dt);
return ds;
}
}
不完全漂亮,但会输出:
Ansvarig,Name,Week 4,Week 5,Week 6,Week 7,Week 12,Week 13,
John Doe,John Andersson,29,0,,,,8,
John Doe,Anders Cameron,8,,,,11,,
John Doe,Steve Smith,8,,0,0,,,