我有一张格式如下的表:
---------------------------------------------
|name | date | timeTable |clockIn|clockOut|
---------------------------------------------
|jhon |01/02/15| Mornning | 08:29 |______ |
---------------------------------------------
|jhon |01/02/15| Afternoon |_______| 04:31 |
---------------------------------------------
|Harry|01/02/15| Mornning | 08:23 |_______ |
---------------------------------------------
|Harry|01/02/15| Afternoon |_______| 04:29 |
---------------------------------------------
从上表中,我想要的格式为:
---------------------------------------------
|name | date | clockIn|clockOut|
---------------------------------------------
|jhon |01/02/15| 08:29 | 04:31 |
---------------------------------------------
|Harry|01/02/15| 08:23 | 04:29 |
---------------------------------------------
我的工作如下: 控制器
var t1 = (from a in hc.attendanceLogs
where (a.timeTable == "Morning")
select new
{
name = a.name,
date = a.date,
timeTable = a.timeTable,
clockIn = a.clockIn
}).ToList();
var t2 = (from a in hc.attendanceLogs
where a.timeTable == "Afternoon"
select new
{
date = a.date,
clockOut = a.clockOut
}).ToList();
DataTable finalTable = new DataTable();
finalTable.Columns.Add("name", typeof(string));
finalTable.Columns.Add("date", typeof(string));
finalTable.Columns.Add("clockIn", typeof(string));
finalTable.Columns.Add("clockOut", typeof(string));
var t3 = from a in t1
join d in t2
on
a.date equals d.date
select
finalTable.LoadDataRow(
new object[]
{ a.name, a.date, a.clockIn, d.clockOut },
false);
ViewBag.Data = finalTable;
我尝试做的是,剪切初始表hc.attendanceLogs
的前4列并将其弹出变量t1
。然后,我获取初始表hc.attendanceLogs
的最后一列,并将其弹出变量t2
。最后,将t1与t2连接到finalTable
,其中t1中的日期等于t2中的日期。
当我尝试在视图中显示ViewBag.Data
时,问题是finalTable
为空。
我出错的任何想法?
答案 0 :(得分:1)
我认为这是因为你实际上并没有在t3中进行评估或迭代。
尝试添加.ToList()
和var t3 == ...
行的结尾。