Is there a faster way to add rows in C# dictionary other than dictionary.add? -
we using dictionary key value pair both string, dictionary has millions of rows been added in foreach loop. profiler results show dictionary.add taking elapsed time in minutes million plus hits.
below method reference.
public virtual dictionary<string, string> createdictionaryrow(string[] row) { dictionary<string, string> dictionaryrow = new dictionary<string, string>(); foreach (columnmappingbo item in this.layout.items) { string value = string.empty; if (!string.isnullorempty(item.columnposition)) { if (item.columnposition == "zz") { value = string.empty; } else { if (layoutposition.trygetvalue(item.columnid, out ordinal)) { if (row.length > ordinal) { if (row[ordinal] != null) { value = row[ordinal]; } } } } } dictionaryrow.add(item.columnnameid, value); } return dictionaryrow; }
all code lines fine, performance impacted when dictionaryrow.add() executed. let know how optimise adding rows dictionary, external custom library can used ?
i try changing linq query.
maybe this:
public dictionary<string, string> createdictionaryrow(string[] row) { return this.layout.items.todictionary(item => item.columnnameid, item => getvalue(item, row));; } private string getvalue(columnmappingbo item, string[] row) { string value = string.empty; if (!string.isnullorempty(item.columnposition)) { if (item.columnposition == "zz") { value = string.empty; } else { if (layoutposition.trygetvalue(item.columnid, out ordinal)) { if (row.length > ordinal) { if (row[ordinal] != null) { value = row[ordinal]; } } } } } return value; }
Comments
Post a Comment