我需要比较gammaFactorWeigths和gammaFactors,并确保它们必须具有相同数量的因子,并且在两个输入(指导,计划和目标)中必须具有相同的顺序。
我将下面的输入反序列化。所以我有一份投资组合清单和一份阶段清单。我需要比较投资组合中的gammaFactor和列表列表中的gammaFactorWeigths。有人可以帮助我怎么做吗?我更喜欢没有lambdas的解决方案。
public class AlphaCalcParam
{
public List<PortfolioInputModel> portfolios { get; set; }
public List<PhaseInputModel> phases { get; set; }
public bool Validation(ref string errString)
{
errString = "";
return true;
// check if gammaFactor and gammaFactorWeights match are in the same order
}
}
以下数据结构:
{
"portfolios":[
{
"identifier": "id 1",
"gammaFactors":[
{
"factorIdentifier":"Guidance",
"factorOffered":1
},
{
"factorIdentifier":"Planning",
"factorOffered":0
},
{
"factorIdentifier":"Goal",
"factorOffered":0
}
]
},
{
"identifier": "id 2",
"gammaFactors":[
{
"factorIdentifier":"Guidance",
"factorOffered":1
},
{
"factorIdentifier":"Planning",
"factorOffered":1
},
{
"factorIdentifier":"Goal",
"factorOffered":1
}
]
}
],
"phases":[
{
"identifier": "xyz",
"gammaFactorWeights":[
{
"factorIdentifier":"Guidance",
"factorWeight":0.0075
},
{
"factorIdentifier":"Planning",
"factorWeight":0.003
},
{
"factorIdentifier":"Goal",
"factorWeight":0.0015
}
]
},
{
"identifier": "xyz",
"gammaFactorWeights":[
{
"factorIdentifier":"Guidance",
"factorWeight":0.005
},
{
"factorIdentifier":"Planning",
"factorWeight":0.0025
},
{
"factorIdentifier":"Goal",
"factorWeight":0.0015
}
]
},
{
"identifier": "xyz",
"gammaFactorWeights":[
{
"factorIdentifier":"Guidance",
"factorWeight":0.0025
},
{
"factorIdentifier":"Planning",
"factorWeight":0.002
},
{
"factorIdentifier":"Goal",
"factorWeight":0.0015
}
]
}
]
}
答案 0 :(得分:1)
这是我想到的一种方法。
int length = Math.Min(portfolios.Count, phases.Count); // Only if collections can be of different length
for (int i = 0; i < length; i++)
{
var portfolio = portfolios[i];
var phase = phases[i];
var factors = portfolio.gammaFactors;
var weights = phase.gammaFactorWeights;
var factorOffered = int.MaxValue;
for (int j = 0; j < factors.Count; j++)
{
if (factors[j].factorOffered > factorOffered)
{
// Next factor is greater than current which fails validation.
return false;
}
factorOffered = factors[j].factorOffered;
}
// Same can be done for weights.
}