i have prepared an example:
I have a core dictionary _keyValueResultFromRefactoring and dictionary with the name of _wordTable.
Now I want to take values from dictionary _keyValueResultFromRefactoring to init values for the other dictionary _wordTable.
Actually transform one dictionary partyl into the other dictionary, by creating new objects of it.
The main work happens in SetWordTabelle:
protected void SetWordTabelle(Dictionary<string, List<MyCustomClass>> myReducedList, string nameOfListToSearchFor)
{
Dictionary<string, Dictionary<string, List<MyCustomClass>>> preWordTable = _keyValueResultFromRefactoring.Where(x => x.Key.Equals(nameOfListToSearchFor))
.ToDictionary(x => x.Key,
y =>
{
if (y.Value != null && y.Value is List<Dictionary<string, object>>)
{
List<Dictionary<string, object>> tempValue = y.Value as List<Dictionary<string, object>>;
Dictionary<string, List<MyCustomClass>> finalResult = new Dictionary<string, List<MyCustomClass>>();
// check against values of "myReducedList", that match my criteria
foreach (KeyValuePair<string, List<MyCustomClass>> reducedListItem in myReducedList)
{
List<MyCustomClass> tempRestultValue = new List<MyCustomClass>();
#region The actual work
foreach (MyCustomClass reducedListItemCustomClass in reducedListItem.Value) //geht jede einzelne von mir vorbereitet Klasse/object durch, "$Table1"
{
List<string> tempStringArray = new List<string>();
double tempCalcColumnClaculator = 0;
double tempCalcColumnResult = 0;
#region Filling sting[], the actual column
foreach (var item in tempValue) //inside "List", 51 count
{
foreach (var subItem in item)//inside "Dictionary"
{
if (subItem.Key.Equals(reducedListItemCustomClass.MyKey))//match "myReducedList" with "_keyValueResultFromRefactoring"
{
if (!reducedListItemCustomClass.MySumCoumn)
{
tempStringArray.Add(subItem.Value.ToString());
}
else
{
if (Double.TryParse(subItem.Value != null ? subItem.Value.ToString() : "", out tempCalcColumnClaculator))
{
tempCalcColumnResult += tempCalcColumnClaculator;
}
}
}
}
}
#endregion
//the last record will be the calculated result
if (reducedListItemCustomClass.MySumCoumn)
{
tempStringArray.Add(tempCalcColumnResult.ToString());
}
tempRestultValue.Add(new MyCustomClass(reducedListItemCustomClass.MyKey, reducedListItemCustomClass.MyHeading, tempStringArray.ToArray()));
}
#endregion
finalResult.Add(reducedListItem.Key, tempRestultValue);
}
retu finalResult;
}
else
{
throw new ApplicationException("y.Value is not valid");
}
});
//_wordTable
_wordTable = preWordTable;
Debug.WriteLine(_wordTable);
}
The code I have written so far works, but now I want to change the it LINQ/Lambda, to make it more performant.
How to do this ?
By the way I think it is not that performant to use so many foreach, like I did.
Actually preWordTable should not retu its result to Dictionary<string, Dictionary<string, List<MyCustomClass>>>, but
Dictionary<string, List<MyCustomClass>> and the result should be save to _wordTable.
