4

I have two tables in my database, energyinfo (t1) and enerfyinfometers (t2).

I have a start and stop time in t1 like 500 min difference.

I want to add the time in between t1 (start and stop) for every one minute in t2.

Here's my code. it takes a lot of time to run and t1 entries is happening but fails to do t2.

if (dbContext == null)
{
    dbContext = Context.Create("d:\\temp\\new.sdf", "");

    var start = DateTime.Now;
    DateTime time = DateTime.Now;
    DateTime time2 = DateTime.Now;

    // time = time + TimeSpan.FromMinutes(30);
    time2 = time2 + TimeSpan.FromMinutes(1);

    Random rnd = new Random();
    double Counter = 50;
    var stop = start.AddMinutes(15);

    double value2 =36;

    for (int i = 1; i < 36; i++)
    {
        Counter = Counter + 0.5; 
        double value3 = rnd.Next (2,12) +0.5;
        double value4 = value3 / 2;
        double value = rnd.Next(50) +  0.5;

        value2 += (value / 60);
        double roundedValue = Math.Ceiling(value2) + 1;
        int LMH = rnd.Next(0, 3);

        dbContext.EnergyInfo.Add(new EnergyInfo() 
                                     { EId = i, 
                                       Timestamp = time, type = LMH, 
                                       startTime = start, stopTime = stop, 
                                       demandCharge = value3, 
                                       threshHold = 70, 
                                       normalCharge = value4, 
                                       peakDuration = 900 });

        dbContext.SaveChanges();

        for (var x = start; x < stop; x.AddMinutes(1))
        {
            var ob = new EnergyMeterInfo() { Timestamp = x, MeterId = 5, 
                                             powerConsumptionKW = Counter, 
                                             cumlativePwrConsumption = roundedValue, 
                                             EnergyInfoId = i };

            using (dbContext.Database.BeginTransaction())
            {
                dbContext.EnergyMeterInfo.Add(ob);
            }
        }

        start = stop;
        stop = start.AddMinutes(500);

        dbContext.SaveChanges();
    }
}

MessageBox.Show(dbContext.EnergyInfo.Count() + " Records found !" + dbContext.EnergyMeterInfo.Count() + " found");
5
  • What is the error message? Commented Jul 12, 2015 at 7:19
  • @MikeEason no error!, Code keeps running for a long time with out any results in DataBase. Commented Jul 12, 2015 at 7:22
  • error in the Loop for (var x = start; x < stop; x.AddMinutes(1)) Commented Jul 12, 2015 at 7:26
  • 1
    In your for loop declaration, you are calling x.AddMinutes(1). This is effectively adding 1 minute every time the loop has completed, therefore x will never be greater than stop. Looks like you need to rethink your for loop logic. Commented Jul 12, 2015 at 7:40
  • The loop logic is ok. Just the loop variable isn't incremented. Commented Jul 12, 2015 at 8:54

1 Answer 1

2

x.AddMinutes(1) does not change the value of x. Instead it returns a new DateTime instance, which you should then assign to x.

The loop should therefore look like this:

for (var x = start; x < stop; x = x.AddMinutes(1))
{
    ...
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.