【发布时间】:2021-12-02 14:13:40
【问题描述】:
我有以下数据列表。
var data = new List<SummaryTabData>()
{
new SummaryTabData(){ LP = "LP_1", assetClass = "Asset_CFD_Long", avgSlippagePts = 54.23} ,
new SummaryTabData(){ LP = "LP_1", assetClass = "Asset_FX_Long", avgSlippagePts = 61.41} ,
new SummaryTabData(){ LP = "LP_1", assetClass = "Asset_FX_Short", avgSlippagePts = 11.48} ,
new SummaryTabData(){ LP = "LP_1", assetClass = "Asset_CFD_Short", avgSlippagePts = 63.51} ,
new SummaryTabData(){ LP = "LP_2", assetClass = "Asset_FX_Long", avgSlippagePts = 6.51} ,
new SummaryTabData(){ LP = "LP_2", assetClass = "Asset_FX_Short", avgSlippagePts = 13.51}
};
我的目标是为每种类型的 LP 的每对 Asset_CFD_long/Asset_CFD_short 和 Asset_FX_Long/Asset_FX_Short 添加一个额外的行。有时可能有 2 或 4 个资产类别,就像您在示例片段中看到的那样
最终的结果应该是这样的。
var data = new List<SummaryTabData>()
{
new SummaryTabData(){ LP = "LP_1", assetClass = "Asset_CFD_Long", avgSlippagePts = 54.23} ,
new SummaryTabData(){ LP = "LP_1", assetClass = "Asset_CFD_Short", avgSlippagePts = 63.51} ,
// that is the new row added to the collection for LP_1 CFD
new SummaryTabData(){ LP = "LP_1", assetClass = "CFD_Total", avgSlippagePts = 117.74} ,
new SummaryTabData(){ LP = "LP_1", assetClass = "Asset_FX_Long", avgSlippagePts = 61.41} ,
new SummaryTabData(){ LP = "LP_1", assetClass = "Asset_FX_Short", avgSlippagePts = 11.48} ,
// that is the new row added to the collection for LP_1 FX
new SummaryTabData(){ LP = "LP_1", assetClass = "FX_Total", avgSlippagePts = 72.89} ,
new SummaryTabData(){ LP = "LP_2", assetClass = "Asset_FX_Long", avgSlippagePts = 6.51} ,
new SummaryTabData(){ LP = "LP_2", assetClass = "Asset_FX_Short", avgSlippagePts = 13.51},
// that is the new row added to the collection for LP_2 FX
new SummaryTabData(){ LP = "LP_2", assetClass = "FX_Total", avgSlippagePts = 20.02},
};
数据未排序。您是否知道可以执行此操作的某种 LINQ 查询,或者我应该使用传统的 for 循环方法?
【问题讨论】:
-
要求有点奇怪。但是我不判断。基本上你会想要
Groupby然后Sum,。然后迭代结果,然后返回列表,找到每个组的最后一次出现,然后插入。 -
@TheGeneral 你能提供一个代码 sn-p 吗?
-
如果没有排序,你怎么知道哪两个项目连接在一起?
标签: c# linq linq-to-sql