part of the code:
Dictionary<Calculation, List<PropertyValue>> result = new Dictionary<Calculation, List<PropertyValue>>();
while (reader != null && reader.Read()) //it loops about 60000, and it will be bigger
{
#region create calc and propvalue variables
//...
#endregion
//this FirstOrDefault needs a lot of time
tmpElementOfResult = result.Keys.FirstOrDefault(r => r.InnerID == calc.InnerID);
if (tmpElementOfResult == null)
{
result.Add(calc, new List<PropertyValue> { propValue });
}
else
{
result[tmpElementOfResult].Add(propValue);
}
}
Could you give me some idea how to make it faster, because now it’s approximately 25 sec 🙁 ?
It sounds like you should have a dictionary from the type of
calc.InnerID, instead of aDictionary<Calc, ...>. That way you can do the lookup far more quickly. Do you actually need to store theCalcitself at all, or are you only interested in the ID?For example:
Alternatively, if you can convert the reader to an
IEnumerable<Calc>you could use:EDIT: It sounds like two Calc values should be deemed equal if they have the same
InnerID, right? So overrideEqualsandGetHashCodewithinCalcto refer to theInnerID. Then you can just use:… or you can use code like the first snippet, but with a
Dictionary<Calc, ...>: