我想List<Dictionary<String,Object>>
从原始列表字典值( )中提取处理后的字典值列表( List<Dictionary<String,Object>>
)。
原始字典可能包含字符串/数值
例如:
Dictionary<String, Object> rawListDict = new Dictionary<String, Object>();
rawListDict.Add("Product","Apple");
rawListDict.Add("Region", "West");
rawListDict.Add("Profit", 90);
原始清单:
苹果西 90
苹果东 10
苹果西 80
处理清单:
苹果西 170
苹果东 10
考虑一个包含具有相同产品和区域的字典的列表,我想要一个字典,当“产品”和“区域”相同时添加“利润”。(即)具有相似项目的字典列表被分组为单个字典,没有任何重复
注意:原始列表可以超过 30K 条目。:-(
我已经通过蛮力技术实现了一个逻辑,它消耗了大量的内存和时间。有没有办法以 LINQ 风格或任何其他方法来减少时间和内存?
编辑:我更喜欢字典,因为成员/键的数量仅在运行时才知道。
我已经实现的代码:
//Get fields which could be used for combining values
var nonMeasurableFields = report.datagrid_fields.
Where(field => field.dataType.Equals(ImFieldDatatype.STRING_VALUE) || field.dataType.Equals(ImFieldDatatype.DATE_VALUE)).
Select(field => field.name).ToList();
if (nonMeasurableFields != null && nonMeasurableFields.Count > 0)
{
#region Outer For Loop
for (int index = 0; index < processedData.Count; index++)
{
var baseDict = processedData.ElementAt(index);
Dictionary<String, Object> compareDict = null;
#region Recursive Loop
for (int recursiveIndex = index + 1; recursiveIndex < processedData.Count; recursiveIndex++)
{
compareDict = processedData.ElementAt(recursiveIndex);
int matchesCount = 0;
#region comparison logic
foreach (var key in nonMeasurableFields)
{
var baseDictValue = baseDict[key];
var compareDictValue = compareDict[key];
if (baseDictValue == null && compareDictValue == null)
{
matchesCount++;
}
else
{
if (baseDictValue != null && compareDictValue == null)
{
matchesCount = 0;
}
else if (baseDictValue == null && compareDictValue != null)
{
matchesCount = 0;
}
else if (baseDictValue != null && compareDictValue != null)
{
if (baseDictValue.Equals(compareDictValue))
{
matchesCount++;
}
else
{
matchesCount = 0;
}
}
}
}
#endregion
#region If Match -- Combine
if (matchesCount == nonMeasurableFields.Count)
{
#region combine logic
// Combine the two dictionary ..
processedData.Remove(baseDict);
processedData.Remove(compareDict);
// combine the base and compare dict
Dictionary<String, Object> combinedDict = new Dictionary<string, object>();
var keyNeededInDict = baseDict.Keys.ToList();
foreach (var key in keyNeededInDict.ToList())
{
if (nonMeasurableFields.Contains(key))
{
combinedDict.Add(key, baseDict[key]);
}
else
{
Object value = Convert.ToDouble(baseDict[key]) + Convert.ToDouble(compareDict[key]);
combinedDict.Add(key, value);
}
}
processedData.Add(combinedDict);
index = -1; // Resetting the looping index so that the merging works for all values
recursiveIndex = -1; // Ensuring all the values are considered at least once whenever
// a change is made to the list (i.e merging the dict)
break;
#endregion
}
else
{
// No matches
// continue to next
}
#endregion
}
#endregion
}
#endregion
}
注意: 我将知道哪个键(键的值)是字符串类型和数字类型的信息。提供的示例仅用于演示目的。键和值仅在运行时才知道。如果字符串值相等,我应该合并两个字典。我将在合并时添加数值。
编辑2: 列表中的所有字典都将具有相同的键没有值将被丢弃。具有相同值的字典将被合并。