# get better performance [closed]

I have this code that works, but I'm sure that it can be done better, any suggestions?

    Dictionary<string, StrainGroupings> GetOperationStrainGroupings(string locale)
{
var result = new Dictionary<string, StrainGroupings>();

try
{
Open();
setDatabaseTables();
result = Connection.Table<StrainGroupings>()
.Where(sg => sg.Locale == locale)
.Join(Connection.Table<OperationStrainGroups>(),
strainGrouping => strainGrouping.StrainGroupName,
operationStrainGroup => operationStrainGroup.StrainGroupName,
(strainGrouping, operationStrainGroup) => new { Name = operationStrainGroup.OperationName.Distinct(), sg = strainGrouping })
.AsEnumerable()
.ToDictionary(row => row.Name.ToString(), row => row.sg);
}
catch (Exception ex)
{
throw new ApplicationException("Error retrieving operation strain groupings", ex);
}
finally { Close(); }
return result;
}


It is part of a method that returns a Dictionary(). The idea is return result, as it is dealing with a sQLite Database, it is in a try/catch/finally. That's why I'm not returning result directly, I need to close the database first and maybe dealing with exceptions.

• This question lacks any indication of what the code is intended to achieve. To help reviewers give you better answers, please add sufficient context to your question, including a title that summarises the purpose of the code. We want to know why much more than how. The more you tell us about what your code is for, the easier it will be for reviewers to help you. The title needs an edit to simply state the task. – Toby Speight Jan 16 at 10:49
• The code provided is not sufficient to meaningfully review. A single code line cannot be judged as appropriate (or not) in an otherwise unmentioned codebase. – Flater Jan 16 at 11:14
• Also, knowing the underlying technology is indispensable. It seems to be LINQ-to-SQL. It would also help to post the classes (StrainGroupings etc.) and approx. number of records. Performance problems tend to be very hard to tackle without lots of internal details. Meaning: it tends to be impossible. – Gert Arnold Jan 16 at 14:13
• I'm just after the code, I have no control over the database and I don't know how many records there are. I can't change the class either – Iria Jan 16 at 14:19
• There is only one way to reliably improve performance. (1) consult with stakeholders to determine the metric for performance. (2) consult with stakeholders to determine acceptable and desirable bars for performance (3) Construct a device which measures those metrics, (4) are you better than acceptable? If yes, go to the beach. (5) If not, apply your metric to every subsystem to determine the one that is most out of compliance with the metric, (6) make a change you believe moves the metric, and test it; repeat until you find an improvement, (7) go back to step 4. – Eric Lippert Jan 29 at 23:39