正如您所看到的,我正在尝试初始化包含动态数组动态数组的字典。有人能给我一个更干净的方法吗?必须有一种方法可以使用for循环。
// Sizes: 20X, 40X, W, M, WM, Other
sizesDict.Add(eDate, new dynamic[6] { // Date, Size
new dynamic[6] { // Data
/* RangeMax */
new List<decimal> { r.rateEvalDLoc.rateEvalOLoc.charge.Total },
/* RangeMin */
new List<decimal> { r.rateEvalDLoc.rateEvalOLoc.charge.Total },
/* OriginMax */
new List<string> { r.rateEvalDLoc.originLoc.LocationName.ToString() },
/* OriginMin */
new List<string> { r.rateEvalDLoc.originLoc.LocationName.ToString() },
/* DestinationMax */
new List<string> { r.destinationLoc.LocationName.ToString() },
/* DestinationMin */
new List<string> { r.destinationLoc.LocationName.ToString() }
},
new dynamic[6] { // Data
/* RangeMax */
new List<decimal> { r.rateEvalDLoc.rateEvalOLoc.charge.Total },
/* RangeMin */
new List<decimal> { r.rateEvalDLoc.rateEvalOLoc.charge.Total },
/* OriginMax */
new List<string> { r.rateEvalDLoc.originLoc.LocationName.ToString() },
/* OriginMin */
new List<string> { r.rateEvalDLoc.originLoc.LocationName.ToString() },
/* DestinationMax */
new List<string> { r.destinationLoc.LocationName.ToString() },
/* DestinationMin */
new List<string> { r.destinationLoc.LocationName.ToString() }
},
new dynamic[6] { // Data
/* RangeMax */
new List<decimal> { r.rateEvalDLoc.rateEvalOLoc.charge.Total },
/* RangeMin */
new List<decimal> { r.rateEvalDLoc.rateEvalOLoc.charge.Total },
/* OriginMax */
new List<string> { r.rateEvalDLoc.originLoc.LocationName.ToString() },
/* OriginMin */
new List<string> { r.rateEvalDLoc.originLoc.LocationName.ToString() },
/* DestinationMax */
new List<string> { r.destinationLoc.LocationName.ToString() },
/* DestinationMin */
new List<string> { r.destinationLoc.LocationName.ToString() }
},
new dynamic[6] { // Data
/* RangeMax */
new List<decimal> { r.rateEvalDLoc.rateEvalOLoc.charge.Total },
/* RangeMin */
new List<decimal> { r.rateEvalDLoc.rateEvalOLoc.charge.Total },
/* OriginMax */
new List<string> { r.rateEvalDLoc.originLoc.LocationName.ToString() },
/* OriginMin */
new List<string> { r.rateEvalDLoc.originLoc.LocationName.ToString() },
/* DestinationMax */
new List<string> { r.destinationLoc.LocationName.ToString() },
/* DestinationMin */
new List<string> { r.destinationLoc.LocationName.ToString() }
},
new dynamic[6] { // Data
/* RangeMax */
new List<decimal> { r.rateEvalDLoc.rateEvalOLoc.charge.Total },
/* RangeMin */
new List<decimal> { r.rateEvalDLoc.rateEvalOLoc.charge.Total },
/* OriginMax */
new List<string> { r.rateEvalDLoc.originLoc.LocationName.ToString() },
/* OriginMin */
new List<string> { r.rateEvalDLoc.originLoc.LocationName.ToString() },
/* DestinationMax */
new List<string> { r.destinationLoc.LocationName.ToString() },
/* DestinationMin */
new List<string> { r.destinationLoc.LocationName.ToString() }
},
new dynamic[6] { // Data
/* RangeMax */
new List<decimal> { r.rateEvalDLoc.rateEvalOLoc.charge.Total },
/* RangeMin */
new List<decimal> { r.rateEvalDLoc.rateEvalOLoc.charge.Total },
/* OriginMax */
new List<string> { r.rateEvalDLoc.originLoc.LocationName.ToString() },
/* OriginMin */
new List<string> { r.rateEvalDLoc.originLoc.LocationName.ToString() },
/* DestinationMax */
new List<string> { r.destinationLoc.LocationName.ToString() },
/* DestinationMin */
new List<string> { r.destinationLoc.LocationName.ToString() }
}
});
答案 0 :(得分:2)
不确定为什么要使用动态,但你可以这样做:
创建一个函数来创建一个最内部动态数组:
private static dynamic[] GetDataArray(
decimal rangeMax,
decimal rangeMin,
string originMax,
string originMin,
string destMax,
string destMin)
{
return new dynamic[6] { // Data
/* RangeMax */
new List<decimal> { rangeMax },
/* RangeMin */
new List<decimal> { rangeMin },
/* OriginMax */
new List<string> { originMax },
/* OriginMin */
new List<string> { originMin },
/* DestinationMax */
new List<string> { destMax },
/* DestinationMin */
new List<string> { destMin }
};
}
然后调用该函数6次将每个动态数组添加到最外面的动态数组。最后,将该数组添加到您的字典中。
dynamic[] allData = new dynamic[6];
for (int i = 0; i < allData.Length; i++)
{
allData[i] = GetDataArray(
r.rateEvalDLoc.rateEvalOLoc.charge.Total,
r.rateEvalDLoc.rateEvalOLoc.charge.Total,
r.rateEvalDLoc.originLoc.LocationName.ToString(),
r.rateEvalDLoc.originLoc.LocationName.ToString(),
r.destinationLoc.LocationName.ToString(),
r.destinationLoc.LocationName.ToString());
}
sizesDict.Add(eDate, allData);
如果RangeMax总是等于RangeMin,并且OriginMax == OriginMin和DestinationMax == DestinationMin,则可以将函数简化为只有3个参数。此外,如果所有最内部动态数组都具有相同的内容,您可以进一步简化它。