为了解释问题,我创建了一个简化的示例。在现实生活中,数据类有些复杂。考虑以下数据类:
public class Data
{
public Data(string source, string path, string information)
{
this.Source = source;
this.Path = path;
this.Information = information;
}
public string Source { get; set; }
public string Path { get; set; }
public string Information { get; set; }
}
现在考虑以下数组:
var array = new Data[] {
new Data("MS", @"c:\temp\img1.jpg", "{a}"),
new Data("IBM", @"c:\temp\img3.jpg", "{b}"),
new Data("Google", @"c:\temp\img1.jpg", "{c}"),
new Data("MS", @"c:\temp\img2.jpg", "{d}"),
new Data("MS", @"c:\temp\img3.jpg", "{e}"),
new Data("Google", @"c:\temp\img1.jpg", "{f}"),
new Data("IBM", @"c:\temp\img2.jpg", "{g}")
};
我想通过对进行分区并对上的Path
每个分区进行排序来处理数据Source
。输出需要像这样:
c:\temp\img1.jpg
"Google": "{c}"
"IBM": "{f}"
"MS": "{a}"
c:\temp\img2.jpg
"IBM": "{g}"
"MS": "{d}"
c:\temp\img3.jpg
"IBM": "{b}"
"MS": "{e}
如何使用LINQ创建这些分区?
在这里您可以使用以下代码:https : //dotnetfiddle.net/EbKluE
您可以使用LINQOrderBy
和GroupBy
来对项目进行排序,Source
并按以下顺序对订购的项目进行分组Path
:
var partitioned = array
.OrderBy(data => data.Source)
.GroupBy(data => data.Path);
看到这个小提琴演示。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句