entity-framework - LINQ 转换/透视
问题描述
我正在做一个项目,我使用实体框架将信息保存在简单的 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<ClassA> 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<Test>
{
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<Test2>();
var currCat = -1;
var currVal = -1;
for (var i = 0; i < elNo; 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}";
}
推荐阅读
- python - FastAPI中音频流的Websockets桥接器
- ruby-on-rails - 在 dockerized Ruby On Rails 应用程序中安装/升级 postgresql-client 到版本 12
- image - Flutter & Shared Preference:如何将网络图像保存到本地内存?
- typescript - 相同长度的参数打字稿
- docker - Docker 构建不共享 MAC 的网络接口
- python - Python Youtube API:UnicodeEncodeError:'charmap'编解码器无法在位置 7741 编码字符'\u279c':字符映射到
- sql - 如何在插入sql表之前检查条目是否存在
- arduino - 关于将代码加载到 UNO 板的问题
- python - 从 .py 文件中添加/删除自定义 Kivy 小部件
- angular - 使用Angular按下后退按钮时如何导航到另一条路线