情况是,我需要在网格视图中创建表格,如下所示:
----------| ID---|---姓名---|--1/2002--|--2/2002--|--1/2003--|........| 2/2009 |
客户1--|
客户2--|
:
:
我在 db 中有两个表 - 客户和订单,通过 LINQ to SQL DataContext
ID 和我从简单查询中获得的客户名称
var custInfo = from cust in db.Customers
select new { ID = cust.Id,
FullName = cust.FirstName + " " + cust.LastName }
dataGridOrdersPreview.DataSource = custInfo;
我需要一些线索,如何生成格式为 t/year 的列,其中 t 表示今年上半年或下半年,并将该年该会话中每个客户的订单分配给生成的列(仅显示成本)
[编辑]
就目前而言,我正在尝试这样的事情:
var orders = from ord in db.Orders
group ord by ord.Id_cust into grouped
let costs = grouped
.Where( s => s.YearSession == session && s.Year == year)
.Select(a => new { Costs = a.Cost ) } )
select new { ID = grouped.Key,
Name = custInfo
.Where( a => a.ID == grouped.Key)
.Select( j => j.Name).Single(),
Cost = ExtensionLibrary.Sum(costs, "\n")
};
(在成本中仅获取每个客户在该年度会话中的总成本)
然后我考虑遍历这些年和会话,并以某种方式将查询结果获取到相应的列
while (year <= DateTime.Today.Year)
{
year++;
while (session < 2)
{
session++;
dataGridOrdersPreview.Columns.Add(session +"/"+ year);
col.Add((session +"/"+ year),
orders.Select( a => a.Cost ).ToList() );
/* col is Dictionary<string, List<string> > */
}
session = 0;
}
在这里,我生成了我想要的列,并且我在 Dictionary 中有订单,其中 Key 是列名,Value 是该列中的订单,但我需要一些帮助将其绑定到该列