1
\$\begingroup\$

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.

\$\endgroup\$
6
  • 4
    \$\begingroup\$ 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. \$\endgroup\$ Commented Jan 16, 2020 at 10:49
  • \$\begingroup\$ 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. \$\endgroup\$
    – Flater
    Commented Jan 16, 2020 at 11:14
  • \$\begingroup\$ 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. \$\endgroup\$ Commented Jan 16, 2020 at 14:13
  • \$\begingroup\$ 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 \$\endgroup\$
    – Iria
    Commented Jan 16, 2020 at 14:19
  • \$\begingroup\$ 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. \$\endgroup\$ Commented Jan 29, 2020 at 23:39

0

Browse other questions tagged or ask your own question.