尝试使用LINQ透视数据 [英] Trying to pivot data using linq
问题描述
我有下面的类结构:
公共类任务日志
{
公众的DateTime LOGDATE {得到;组; }
公共字符串机器人{搞定;组; }
公共字符串任务{搞定;组; }
公共Enums.ProbeDataState国家{搞定;组; }
公共任务日志()
{
}
}
我创建了一个通用的清单如下:
列表<&任务日志GT;日志;
我的输出:
机器人最新状态
------------------------------------- --------
AAA 2013年8月5日12:00:00 AM任务:1 = fileDeltaFailed
AAA 2013年8月5日12:00:00 AM任务:2 = fileDeltaFailed
AAA 2013年8月5日上午12点00分00秒任务:4 = fileDeltaFailed
BBB 2013年8月5日上午12点00分00秒任务:1 = fileDeltaFailed
BBB 8/5 / 2013年上午12时00分00秒任务:2 = fileDeltaFailed
BBB 2013年8月5日上午12时00分00秒任务:4 = fileDeltaFailed
不过,我想组的任务和状态如下:
任务
机器人日期1 2 4
AAA 2013年8月5日12:00:00 AM fileDeltaFailed fileDeltaFailed fileDeltaFailed
BBB 2013年8月5日12:00:00 AM fileDeltaFailed fileDeltaFailed fileDeltaFailed
我尝试使用GROUPBY,没有运气,而且相当坚持。
例如
VAR dataQuery = Logs.Where(W => w.LogDate> =&开始放;&安培; w.LogDate< =完成).GroupBy(G => g.LogDate)。选择(T =>新建
{
LOGDATE = t.Key,
详细= T。排序依据(O => o.Robot)
})了ToList();
如果我明白这个问题,你想要的映射任务状态,每个机器人。你可以通过组机器人,并选择一本字典为每个组:
Logs.GroupBy(T => t.Robot)。选择(G =>新建{
机器人= g.Key,
TaskStates = g.ToDictionary(T => t.Task,T => t.State)
})
这假定任务名称是每个机器人唯一的( ToDictionary $ 。C $ C>将抛出一个异常,否则)
您也可以增加对日期分组的另一个层面:
Logs.GroupBy(T => t.LogDate)。选择(G =>新建{
日期= g.Key,
详细= G。 GROUPBY(T => t.Robot)。选择(G =>新建{
机器人= g.Key,
TaskStates = g.ToDictionary(T => t.Task,T => ; t.State)
})了ToList()
})
请注意,详细信息
属性基本上等同于我的第一个例子中,唯一的区别是它查询外的分组,而不是整个序列。其结果是{日期,详细}其中每一个细节是{机器人,TaskStates}列表的序列。
我没有测试过这一点,所以让我知道,如果有我错过了任何错误。
I have the following class structure:
public class TaskLog
{
public DateTime LogDate { get; set; }
public string Robot { get; set; }
public string Task { get; set; }
public Enums.ProbeDataState State { get; set; }
public TaskLog()
{
}
}
I created a generic list as follows:
List<TaskLog> Logs;
My output:
Robot Date State
---------------------------------------------
aaa 8/5/2013 12:00:00 AM Task:1=fileDeltaFailed
aaa 8/5/2013 12:00:00 AM Task:2=fileDeltaFailed
aaa 8/5/2013 12:00:00 AM Task:4=fileDeltaFailed
bbb 8/5/2013 12:00:00 AM Task:1=fileDeltaFailed
bbb 8/5/2013 12:00:00 AM Task:2=fileDeltaFailed
bbb 8/5/2013 12:00:00 AM Task:4=fileDeltaFailed
However I would like to group the tasks and state as follows:
Tasks
Robot Date 1 2 4
aaa 8/5/2013 12:00:00 AM fileDeltaFailed fileDeltaFailed fileDeltaFailed
bbb 8/5/2013 12:00:00 AM fileDeltaFailed fileDeltaFailed fileDeltaFailed
I tried using the groupby with no luck, and quite stuck.
e.g.
var dataQuery = Logs.Where(w => w.LogDate >= start && w.LogDate <= finish).GroupBy(g => g.LogDate).Select(t => new
{
LogDate = t.Key,
Details = t.OrderBy(o => o.Robot)
}).ToList();
If I understand the question, you want a mapping of Task to State for each Robot. You could group by Robot and select a dictionary for each group:
Logs.GroupBy(t => t.Robot).Select(g => new {
Robot = g.Key,
TaskStates = g.ToDictionary(t => t.Task, t => t.State)
})
This assumes that task names are unique for each robot (ToDictionary
would throw an exception otherwise).
You could also add another level of grouping for dates:
Logs.GroupBy(t => t.LogDate).Select(g => new {
Date = g.Key,
Details = g.GroupBy(t => t.Robot).Select(g => new {
Robot = g.Key,
TaskStates = g.ToDictionary(t => t.Task, t => t.State)
}).ToList()
})
Note that the Details
property is essentially equivalent to my first example, the only difference being that it queries the outer grouping instead of the whole sequence. The result is a sequence of {Date, Details} where each "detail" is a list of {Robot, TaskStates}.
I haven't tested this, so let me know if there are any bugs I missed.
这篇关于尝试使用LINQ透视数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!