我有一个具有以下结构的记录列表:(简化示例!)
class Rate
{
string Code;
double InterestFrom;
double InterestTo;
double IncomeFrom;
double IncomeTo;
double Value;
}
是的,我定义了一个 List
class RateList
{
List<Code> code;
}
class Code
{
string code;
List<Interest> interest;
}
class Interest
{
double InterestFrom;
double InterestTo;
List<Income> income;
}
class Income
{
double IncomeFrom;
double IncomeTo;
double Value;
}
我想用单个 LINQ 查询来完成此操作。我可以制定其他解决方案,但挑战是在一个 LINQ 语句中完成。 基本上,顶级组是按代码分组的。值 InterestFrom 和 InterestTo 的下一层和最低层是 IncomeFrom、IncomeTo 和 Value 的列表。
那么,为此,LINQ 语句是什么?
最佳答案
这是相当大的,但这是我所拥有的:
var rateList = new RateList
{
code = (from r in rates
group r by r.Code into g
select new Code
{
code = g.Key,
interest = (from i in g
group i by new {i.InterestFrom, i.InterestTo} into g2
select new Interest
{
InterestFrom = g2.Key.InterestFrom,
InterestTo = g2.Key.InterestTo,
income = (from inc in g2
select new Income
{
IncomeFrom = inc.IncomeFrom,
IncomeTo = inc.IncomeTo,
Value = inc.Value
}).ToList()
}).ToList()
}).ToList()
};
关于c# - LINQ 中的多级分组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1159233/