我已经在c#中创建了一个返回元组的方法。我很难将结果分配给元组。
我创建了一个名为Tuple benchMarkReturns的Tuple; 如何分配值。目前,当我做一个BenchMarkReturns。 ,我的意思是点,我获得了只读的Item1和Item2属性。
如果有人认为有更好的方法可以提出建议
需要在以下foreach循环中为字段Bmrk1和Bmrk2调用该方法。
我需要将compoundReturnsBenchMark1和compoundReturnsBenchMark2分配给元组
foreach (var p in performance)
{
var model = new TrackRecordVM
{
Year = p.Key,
Jan = (double?)(p.FirstOrDefault(x => x.VALUEDATE.Month == 1) != null ? p.FirstOrDefault(x => x.VALUEDATE.Month == 1).MTD : null),
Feb = (double?)(p.FirstOrDefault(x => x.VALUEDATE.Month == 2) != null ? p.FirstOrDefault(x => x.VALUEDATE.Month == 2).MTD : null),
Mar = (double?)(p.FirstOrDefault(x => x.VALUEDATE.Month == 3) != null ? p.FirstOrDefault(x => x.VALUEDATE.Month == 3).MTD : null),
Apr = (double?)(p.FirstOrDefault(x => x.VALUEDATE.Month == 4) != null ? p.FirstOrDefault(x => x.VALUEDATE.Month == 4).MTD : null),
May = (double?)(p.FirstOrDefault(x => x.VALUEDATE.Month == 5) != null ? p.FirstOrDefault(x => x.VALUEDATE.Month == 5).MTD : null),
Jun = (double?)(p.FirstOrDefault(x => x.VALUEDATE.Month == 6) != null ? p.FirstOrDefault(x => x.VALUEDATE.Month == 6).MTD : null),
Jul = (double?)(p.FirstOrDefault(x => x.VALUEDATE.Month == 7) != null ? p.FirstOrDefault(x => x.VALUEDATE.Month == 7).MTD : null),
Aug = (double?)(p.FirstOrDefault(x => x.VALUEDATE.Month == 8) != null ? p.FirstOrDefault(x => x.VALUEDATE.Month == 8).MTD : null),
Sep = (double?)(p.FirstOrDefault(x => x.VALUEDATE.Month == 9) != null ? p.FirstOrDefault(x => x.VALUEDATE.Month == 9).MTD : null),
Oct = (double?)(p.FirstOrDefault(x => x.VALUEDATE.Month == 10) != null ? p.FirstOrDefault(x => x.VALUEDATE.Month == 10).MTD : null),
Nov = (double?)(p.FirstOrDefault(x => x.VALUEDATE.Month == 11) != null ? p.FirstOrDefault(x => x.VALUEDATE.Month == 11).MTD : null),
Dec = (double?)(p.FirstOrDefault(x => x.VALUEDATE.Month == 12) != null ? p.FirstOrDefault(x => x.VALUEDATE.Month == 12).MTD : null),
YTD = (double?)(p.FirstOrDefault(x => x.VALUEDATE.Month == 12) != null ? p.FirstOrDefault(x => x.VALUEDATE.Month == 12).YTD : null),
Bmrk1 = ,
Bmrk1Name = "Test1",
Bmrk2 = ,
Bmrk2Name = "Test2"
//Bmrk = (double?)(p.FirstOrDefault(x => x.VALUEDATE.Month == 12) != null ? p.FirstOrDefault(x => x.VALUEDATE.Month == 12).BENCHMK1_MTD : null),
//BmrkName = p.FirstOrDefault(x => x.VALUEDATE.Month == 12) != null ? p.FirstOrDefault(x => x.VALUEDATE.Month == 12).BENCHMK1_INDEX_NAME : null
};
返回元组值的方法
private Tuple<double, double> GetBenchMarkByYear(int year, int benchMark1, int benchMark2)
{
Tuple<double, double> benchMarkReturns;
var benchMark1Returns = GetViewService<MV_INDEX_PERFORMANCE>().Where(x => x.Mtd != null && x.IndexId == benchMark1 && x.PriceDate.Year == year).Select(x => x.Mtd).ToArray();
var benchMark2Returns = GetViewService<MV_INDEX_PERFORMANCE>().Where(x => x.IndexId == benchMark2 && x.PriceDate.Year == year).Select(x => x.Mtd).ToArray();
double[] temp1 = benchMark1Returns.Cast<double>().ToArray();
double[] temp2 = benchMark2Returns.Cast<double>().ToArray();
var compoundReturnsBenchMark1 = CompoundReturns(temp1);
var compoundReturnsBenchMark2 = CompoundReturns(temp2);
return benchMarkReturns;
}
答案 0 :(得分:1)
return benchMarkReturns;
benchMarkReturns从未分配。
benchMarkReturns = new Tuple<double, double>(compoundReturnsBenchMark1, compoundReturnsBenchMark2);
将解决您的问题
答案 1 :(得分:0)
代码中的问题是您声明了变量Tuple<double, double> benchMarkReturns
,但从未为其分配值。
因此,这应该是您的方法:
private Tuple<double, double> GetBenchMarkByYear(int year, int benchMark1, int benchMark2)
{
var benchMark1Returns = GetViewService<MV_INDEX_PERFORMANCE>().Where(x => x.Mtd != null && x.IndexId == benchMark1 && x.PriceDate.Year == year).Select(x => x.Mtd).ToArray();
var benchMark2Returns = GetViewService<MV_INDEX_PERFORMANCE>().Where(x => x.IndexId == benchMark2 && x.PriceDate.Year == year).Select(x => x.Mtd).ToArray();
double[] temp1 = benchMark1Returns.Cast<double>().ToArray();
double[] temp2 = benchMark2Returns.Cast<double>().ToArray();
var compoundReturnsBenchMark1 = CompoundReturns(temp1);
var compoundReturnsBenchMark2 = CompoundReturns(temp2);
Tuple<double, double> benchMarkReturns = new Tuple<double, double>(compoundReturnsBenchMark1 , compoundReturnsBenchMark2);
return benchMarkReturns;
}