透视可观察的集合 [英] Pivot an observable collection
本文介绍了透视可观察的集合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在可观察的收藏集中有这样的记录
I have records like this in observable collection
ID Department salary joingdate
1 .NET 5000 04/08/2011
2 .NET 6000 04/07/2011
3 JAVA 7000 04/08/2011
4 JAVA 8000 04/07/2011
5 .NET 9000 04/06/2011
现在我想要新的可观察的集合,例如
now I want new observable collection like
Joingdate .NET(Salary) JAVA(Salary)
04/08/2011 5000 7000
04/07/2011 6000 8000
04/06/2011 9000 NULL
我将如何获得这种可观察的集合?
How would I get this type of observable collection?
推荐答案
假定您具有与您的结构相似的以下结构:
Assuming you have follwing structure that resembles your structure:
public class Data1
{
public int Id { get; set; }
public String Dep { get; set; }
public int Sal { get; set; }
public String JoinDate { get; set; }
}
public class Data2
{
public Data2()
{
Sal = new List<int>();
}
public List<int> Sal { get; set; }
public String JoinDate { get; set; }
public override string ToString()
{
return Sal.Aggregate(JoinDate, (current, s) => current + s.ToString());
}
}
以及以下可观察的集合:
And following observable collections:
public class Data1List : ObservableCollection<Data1>
{
public Data1List()
{
Add(new Data1{ Id = 1, Dep = ".NET", Sal = 5000, JoinDate = "04/08/2011"});
Add(new Data1{ Id = 2, Dep = ".NET", Sal = 6000, JoinDate = "04/07/2011"});
Add(new Data1{ Id = 3, Dep = "JAVA", Sal = 7000, JoinDate = "04/08/2011"});
Add(new Data1{ Id = 4, Dep = "JAVA", Sal = 8000, JoinDate = "04/07/2011"});
Add(new Data1{ Id = 5, Dep = ".NET", Sal = 9000, JoinDate = "04/06/2011"});
}
}
public class Data2List : ObservableCollection<Data2>
{
}
您可以尝试使用以下代码来解决问题,或者至少可以帮助您找到更好的解决方案:
You could try this code to solve your problem or at least get you on the way to a maybe better solution:
var l1 = new Data1List();
var l2 = new Data2List();
foreach (var items in l1.GroupBy(d => d.JoinDate))
{
var d2 = new Data2 { JoinDate = items.Key };
foreach (var item in items)
d2.Sal.Add(item.Sal);
l2.Add(d2);
}
我希望这可以帮助您找到解决方案! :)
I hope this helps you getting to a solution! :)
这篇关于透视可观察的集合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文