获取日期范围内的扣除结果

时间:2016-06-30 07:03:35

标签: c#

我每天都会记录数据使用情况,并希望显示日期范围之间的总数据使用情况。

数据每月10日重置,因此数据使用示例如下:

Number: 001xxxxxxxx 
Date - Data
8th - 100mb
9th - 120mb
10th - 10mb
11th - 40mb

因此,为了在8日和11日之间获得总数据使用量,在第10次重置时,不能将第11次数据扣除第8次(40mb-100mb)。

我需要40mb +(120mb-100mb)= 60mb总使用量

这是我的方法,它将数据,日期和数字提供给Dictionarys

private void getNumberDatas(List<int> phoneNo, DateTime dateStart, DateTime dateEnd)
{
    Dictionary<int, Dictionary<DateTime, float>> d_PhoneNo_DateDataList = new Dictionary<int, Dictionary<DateTime, float>>();
    Dictionary<DateTime, float> d_DateTime_Data = new Dictionary<DateTime, float>();

    string strConnectionString = ConfigurationManager.ConnectionStrings["xxx"].ConnectionString;
    SqlConnection conn = new SqlConnection(strConnectionString);

    string sqlcommand = "SELECT xxx FROM xxx WHERE PhoneNo=@PhoneNo AND date BETWEEN @Date1 AND @Date2";

    for (int i = 0; i < phoneNo.Count; i++)
    {
        d_DateTime_Data.Clear();

        using (SqlCommand cmd = new SqlCommand(sqlcommand, conn))
        {
            conn.Open();
            cmd.Parameters.AddWithValue("@PhoneNo", phoneNo[i]);
            cmd.Parameters.AddWithValue("@Date1", dateStart);
            cmd.Parameters.AddWithValue("@Date2", dateEnd);
            cmd.ExecuteNonQuery();
            SqlDataReader reader = cmd.ExecuteReader();

            while (reader.Read())
            {
                d_DateTime_Data.Add(DateTime.Parse(reader["Date"].ToString()), float.Parse(reader["Data"].ToString()));
            }
            conn.Close();
        }
        d_PhoneNo_DateDataList.Add(phoneNo[i], d_DateTime_Data);
    }
}

因此,获取今天的数据将是:

Dictionary<DateTime, float> temp_Datetime_data = new Dictionary<DateTime, float>();

if (d_PhoneNo_DateDataList.TryGetValue("001xxxxxxxx", out temp_Datetime_data))
{
    float dataToday;

    if (temp_Datetime_data.TryGetValue(DateTime.Today, out dataToday))
    {
        Console.WriteLine(dataToday.ToString());
    }
}

现在我的问题是,如果让用户选择2016年5月20日至2016年6月30日,我不知道如何实施计算

让我知道如果它太混乱,我不知道解释这个的最好方法

2 个答案:

答案 0 :(得分:0)

它并不完美,但它将是您需求的良好基础:

SendEnv LANG LC_*

有了这些数据:

/etc/ssh/ssh_config

我得到private static int getData(Dictionary<DateTime, int> values, DateTime start, DateTime stop) { // Reduce the scope var val = values.Where(x => x.Key >= start && x.Key <= stop); // Create a list of ranges List<Dictionary<DateTime, int>> listOfMonth = new List<Dictionary<DateTime, int>> { new Dictionary<DateTime, int>() }; var currentMonth = listOfMonth.Last(); int previousValue = int.MinValue; foreach (KeyValuePair<DateTime, int> keyValuePair in val) { // Reset the month if (keyValuePair.Value < previousValue) { currentMonth = new Dictionary<DateTime, int>() { {DateTime.Now, 0} // Add placeholder }; listOfMonth.Add(currentMonth); } previousValue = keyValuePair.Value; currentMonth.Add(keyValuePair.Key, keyValuePair.Value); } return listOfMonth.Sum(dictionary => dictionary.Values.Max() - dictionary.Values.Min()); } 120 - 90 )+( 40 - 0 )< / p>

答案 1 :(得分:0)

据我所知,问题可以重述为

  1. 计算用法之间差异的总和,
  2. 除外
  3. 在重置日期,使用金额而不是差额
  4. 出于示例目的,我们可以使用小的使用类

    class Usage
    {
        public Usage(int day, int totalUsage)
        {
            Day = day;
            TotalUsage = totalUsage;
        }
        public int Day { get; }
        public int TotalUsage { get; }
    }
    

    以及表示日志的用法列表

    private readonly static List<Usage> Log = new List<Usage>
    {
        new Usage(8,100),
        new Usage(9,120),
        new Usage(10,10),
        new Usage(11,40),
    };
    

    我们可以按如下方式计算期间的总使用量

    private void CalcTotalUsage(int startDate, int endDate, int expectedTotal)
    {
        // I just used ints for the days for demo purposes, dates will not change the calculation below  
        var records = Log.Where(u => u.Day >= startDate && u.Day <= endDate);
    
        var total = records.Skip(1).Zip(records, (curr, prev) => (curr.TotalUsage > prev.TotalUsage) ? curr.TotalUsage - prev.TotalUsage : curr.TotalUsage).Sum();
    
        Assert.AreEqual(expectedTotal, total);
    
    }
    

    以及检查它是否正常工作的测试

    [TestMethod]
    public void CheckTotals()
    {
        CalcTotalUsage(8, 9, 20);
        CalcTotalUsage(8, 10, 30);
        CalcTotalUsage(8, 11, 60);
    }