获得当前和先前的价值 [英] Getting current and previous value
本文介绍了获得当前和先前的价值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在下面的LINQ查询中,我想添加一个属性,该属性获取先前的 Department
以及当前的 Department
.
In the LINQ query below, I would like to add a property that gets the previous Department
as well as the current Department
.
下面的LINQ查询返回:
The LINQ query below returns:
EffectiveDate EmployeeID Department
11/4/2012 10 0000
1/14/2013 10 9121
2/2/2016 10 9123
如何像这样在当前部门
旁边显示上一个部门
?
How can I show the previous Department
next to current Department
like this?
EffectiveDate EmployeeID Department PreviousDepartment
11/4/2012 10 0000 null
1/14/2013 10 9121 0000
2/2/2016 10 9123 9121
这是当前查询
var users = from s in userTable
where s.EmployeeID == "10"
group new {s} by new { s.EmployeeID, s.Department} into g
select new
{
EffectiveDate = g.Max(m => m.s.EffectiveDate),
EmployeeID = g.Key.EmployeeID,
Department = g.Key.Department
//PreviousDepartment = ???
};
推荐答案
一种方法是将数据存储在内存中,然后像这样修改它:
One way to do it is to store the data in memory, and then modify it like this:
var users = from s in userTable
where s.EmployeeID == "10"
group new {s} by new { s.EmployeeID, s.Department} into g
select new MyClass
{
EffectiveDate = g.Max(m => m.s.EffectiveDate),
EmployeeID = g.Key.EmployeeID,
Department = g.Key.Department
PreviousDepartment = null
};
var result = users.ToList();
for(int i = 1; i < result.Count; i++)
{
result[i].PreviousDepartment = result[i-1].Department;
}
请注意,由于匿名类型属性是只读的,因此代码正在生成 MyClass
的新实例,而不是匿名类型.确保使用正确的属性创建此类.
Please note that the code is generating new instances of MyClass
instead of an anonymous type since anonymous type properties are read-only. Make sure that you create such class with the correct properties.
这篇关于获得当前和先前的价值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文