作者:我就是我 | 来源:互联网 | 2023-09-13 16:58
篇首语:本文由编程笔记#小编为大家整理,主要介绍了LINQ Transform / Pivot相关的知识,希望对你有一定的参考价值。
我正在开发一个项目,我正在使用实体框架在简单的POCO类中保存信息,操作它,然后输出到OpenXML报告。
我有以下示例类:
public class ClassA
{
property string SectionName {get;set;}
property string Status {get;set;}
property int SingleValue {get;set;}
property int CoupleValue {get;set;}
property int FamilyValue {get;set;}
}
这可以填充例如:
SectiOnName= 'Category 1'
Status = 'Opening'
SingleValue = 0
CoupleValue = 0
FamilyValue = 0
SectiOnName= 'Category 1'
Status = 'Current'
SingleValue = 1
CoupleValue = 1
FamilyValue = 1
SectiOnName= 'Category 1'
Status = 'Closing'
SingleValue = 2
CoupleValue = 2
FamilyValue = 2
SectiOnName= 'Category 2'
Status = 'Opening'
SingleValue = 0
CoupleValue = 0
FamilyValue = 0
SectiOnName= 'Category 2'
Status = 'Current'
SingleValue = 1
CoupleValue = 1
FamilyValue = 1
SectiOnName= 'Category 2'
Status = 'Closing'
SingleValue = 2
CoupleValue = 2
FamilyValue = 2
在我的代码中,我有一个ICollection:
ICollection Classes;
有没有办法将此集合转换/转换为使用LINQ输出的准备:
SectionName | ValueLabel | OpeningValue | CurrentValue | ClosingValue
Category 1 | Single | 0 | 1 | 2
Category 1 | Couple | 0 | 1 | 2
Category 1 | Family | 0 | 1 | 2
Category 2 | Single | 0 | 1 | 2
Category 2 | Couple | 0 | 1 | 2
Category 2 | Family | 0 | 1 | 2
答案
你可以想出这样的东西:
var l = new List
{
new Test { SectiOnName= "Cat 1", Status = "Opening", SingleValue = 0, CoupleValue = 0, FamilyValue = 0 },
new Test { SectiOnName= "Cat 1", Status = "Current", SingleValue = 1, CoupleValue = 1, FamilyValue = 1 },
new Test { SectiOnName= "Cat 1", Status = "Closing", SingleValue = 2, CoupleValue = 2, FamilyValue = 2 },
new Test { SectiOnName= "Cat 2", Status = "Opening", SingleValue = 0, CoupleValue = 0, FamilyValue = 0 },
new Test { SectiOnName= "Cat 2", Status = "Current", SingleValue = 1, CoupleValue = 1, FamilyValue = 1 },
new Test { SectiOnName= "Cat 2", Status = "Closing", SingleValue = 2, CoupleValue = 2, FamilyValue = 2 },
};
const string strValue = "Value";
var cats = l.Select(x => x.SectionName).Distinct().ToArray();
var catNo = cats.Length;
var valProps = new Test().GetType().GetProperties().Where(p => p.Name.EndsWith(strValue)).ToArray();
var vals = valProps.Select(p => p.Name.Split(strValue).First()).ToArray();
var valsNo = vals.Length;
var elNo = catNo * valsNo;
var dictOpClCu = l.GroupBy(t => t.Status).ToDictionary(g => g.Key, g => g
.SelectMany(t => valProps.Select(p => (int) p.GetValue(t, null))).ToArray());
var l2 = new List();
var currCat = -1;
var currVal = -1;
for (var i = 0; i {
l2.Add(new Test2
{
Section = cats[i % (elNo / catNo) != 0 ? currCat : ++currCat],
ValueLabel = vals[++currVal % valsNo],
OpeningValue = dictOpClCu["Opening"][i],
ClosingValue = dictOpClCu["Closing"][i],
CurrentValue = dictOpClCu["Current"][i]
});
}
测试类:
public class Test
{
public string SectionName { get; set; }
public string Status { get; set; }
public int SingleValue { get; set; }
public int CoupleValue { get; set; }
public int FamilyValue { get; set; }
}
public class Test2
{
public string Section { get; set; }
public string ValueLabel { get; set; }
public int OpeningValue { get; set; }
public int CurrentValue { get; set; }
public int ClosingValue { get; set; }
public override string ToString() => $"{Section} | {ValueLabel} | {OpeningValue} | {CurrentValue} | {ClosingValue}";
}
另一答案
您可以通过对SectionName
进行分组并将组转换为Status
上的字典来使用LINQ执行此操作:
var ans = Classes.GroupBy(c => c.SectionName)
.Select(cg => new { Key = cg.Key, Values = cg.ToDictionary(c => c.Status) })
.SelectMany(cg => new[] {
new Output { SectiOnName= cg.Key, ValueLabel = "Single", OpeningValue = cg.Values["Opening"].SingleValue, CurrentValue = cg.Values["Closing"].SingleValue, ClosingValue = cg.Values["Current"].SingleValue },
new Output { SectiOnName= cg.Key, ValueLabel = "Couple", OpeningValue = cg.Values["Opening"].CoupleValue, CurrentValue = cg.Values["Closing"].CoupleValue, ClosingValue = cg.Values["Current"].CoupleValue },
new Output { SectiOnName= cg.Key, ValueLabel = "Family", OpeningValue = cg.Values["Opening"].FamilyValue, CurrentValue = cg.Values["Closing"].FamilyValue, ClosingValue = cg.Values["Current"].FamilyValue }
}).ToList();