结合类似的方法

时间:2013-01-17 21:37:29

标签: c# optimization methods

我目前有两种方法:

CalculateDaily()
{
     List<string> tempList;

     // Effective query.  not what is really passed
     tempList = "SELECT timestamp FROM table1 WHERE date = today";

     var total = tempList.Sum();
}

CalculateTotal()
{
     List<string> tempList;

     // Effective query.  not what is really passed
     tempList = "SELECT timestamp FROM table1"

     var total = tempList.Sum();
}

我的问题是我应该将它们分开,还是将它们组合成一个方法并进行if检查是否可行?类似的东西:

Calculate(bool daily)
{
     List<string> tempList;

     if(daily)
          tempList = "SELECT timestamp FROM table1 WHERE date = today";
     else
          tempList = "SELECT timestamp FROM table1";

     var total = tempList.Sum();
}

4 个答案:

答案 0 :(得分:2)

我会使用提供开始日期和结束日期的方法。然后你可以随意使用它。

public static int Calculate(DateTime startDate, DateTime endDate)
{
    string sql = @"SELECT SUM(timestamp) 
                   FROM table1 
                   WHERE date BETWEEN @startDate AND @endDate";
    using(var con=new SqlConnection(connectionString))
    using (var cmd = new SqlCommand(sql, con))
    {
        con.Open();
        cmd.Parameters.AddWithValue("@startDate", startDate);
        cmd.Parameters.AddWithValue("@endDate", endDate);
        int sum = (int)cmd.ExecuteScalar();
        return sum;
    }
}

答案 1 :(得分:1)

我会这样做:

Calculate(bool daily)
{
     List<string> tempList;

     tempList = "SELECT timestamp FROM table1"

     if(daily)
          tempList += " WHERE date = today";

     var total = tempList.Sum();
}

或更多参数化版本(某些伪代码):

Calculate(bool daily)
{
     List<string> tempList;

     tempList = "SELECT timestamp FROM table1 WHERE (@Date IS NULL OR date = @Date)"

     if(daily)
          @Date = today;
     else
          @Date = null;

     var total = tempList.Sum();
}

答案 2 :(得分:1)

怎么样......

Calculate(bool daily)
{
     List<string> tempList;

     tempList = "SELECT timestamp FROM table1";

     if(daily)
         tempList += " WHERE date = today";          

     var total = tempList.Sum();
}

虽然“有效查询”部分需要澄清。

答案 3 :(得分:1)

您可以为标量查询制作一般方法

// Assumes parameter names @0, @1, @2 ... in the query.
public static T ExecuteScalar<T>(string query, params object[] parameters)
{
    using(var conn = new SqlConnection(myConnectionString))
    using (var cmd = new SqlCommand(query, conn)) {
        for (int i = 0; i < parameters.Length; i++) {
            cmd.Parameters.AddWithValue("@" + i, parameters[i]);
        }
        conn.Open();
        return (T)cmd.ExecuteScalar();
    }
}

然后为您的查询创建重载方法

public static decimal SumTable1Amount()
{
    return ExecuteScalar<decimal>("SELECT SUM(amount) FROM table1");
}

public static decimal SumTable1Amount(DateTime date)
{
    return ExecuteScalar<decimal>(
        "SELECT SUM(amount) FROM table1 WHERE date = @0",
        date);
}

public static decimal SumTable1Amount(DateTime fistDate, DateTime lastDate)
{
    return ExecuteScalar<decimal>(
        "SELECT SUM(amount) FROM table1 WHERE date BETWEEN @0 AND @1",
        fistDate, lastDate);
}

现在调用不同的查询非常简单,因此再创建单个参数化方法毫无意义。