dumbel
dumbel

Reputation: 205

c# sorting list <DateTime> and inserting to database

I´ve come to a total stop with my program and im in need of some help.

I got an xml file with customer billings and billing dates. This file has about 4000 billing dates. What i want is to sort them so the once that is in a range of a period date of 2010-04-01 - 2011-03-31 adds to a table column named period1. And the other dates goes to period2 that is 2011-04-01 - 2012-03-31.

Ive been testing and testing this solution in diffrent ways but it wont work. Im adding all the dats to a list named dates. And trying:

if (dates.All(date => date >= startDatePeriod1 && date <= stopDatePeriod1))
{
    adapterBonus.InsertPeriod1Query(// insert to database));
}
else if (dates.All(date => date >= startDatePeriod2 && date >= stopDatePeriod2))
{
    adapterBonus.InsertPeriod2Query(// insert to database));
}

startDatePeriod1 = 2010-04-01
stopDatePeriiod1 = 2011-03-31  

and so on

Upvotes: 0

Views: 608

Answers (6)

R. Martinho Fernandes
R. Martinho Fernandes

Reputation: 234474

The Enumerable.All extension method returns true if every element in the sequence verifies the condition. If dates contains dates in both periods, none of the ifs will run, because both calls to Enumerable.All will return false.

I'm not sure what you mean by "adds to a table column named period1", but if you mean to count all the dates in each period, use Enumerable.Count:

int period1Count = dates.Count(date => date >= startDatePeriod1 && date <= stopDatePeriod1);
int period2Count = dates.Count(date => date >= startDatePeriod2 && date <= stopDatePeriod2);
adapterBonus.InsertPeriod1Query(period1Count);
adapterBonus.InsertPeriod2Query(period2Count);

Upvotes: 3

Antony Scott
Antony Scott

Reputation: 21998

seems like you just need a simple query for each bonus period, something like ...

var period1Dates = date.Where(date => date >= startDate1 && date <= stopDate1);
var period2Dates = date.Where(date => date >= startDate2 && date <= stopDate2);

adapterBonus.InsertPeriod1(period1Dates);
adapterBonus.InsertPeriod2(period2Dates);

As Daniel says, you're checking if all the dates are within each period, which it sound like will never be true, so nothing will be happening.

Upvotes: 1

Michal Ciechan
Michal Ciechan

Reputation: 13888

easiest way to do this is doing a

startDatePeriod1 = 2010-04-01;
stopDatePeriod1 = 2011-03-01;   
startDatePeriod2 = 2011-04-01;
stopDatePeriod2 = 2012-03-01;

foreach(DateTime d in dates) {
    if (d => startDatePeriod1 && d <= startDatePeriod1) {
        adapterBonus.InsertPeriod1Query(// insert to database));
    } else if(d => startDatePeriod2 && d <= stopDatePeriod2) {
        adapterBonus.InsertPeriod2Query(// insert to database));

    }
}

Upvotes: 1

SHug
SHug

Reputation: 688

Try this:

var period1Dates = dates.Where(date => date >= startDatePeriod1 && date <= stopDatePeriod1);
var period2Dates = dates.Where(date => date >= startDatePeriod2 && date >= stopDatePeriod2);

foreach(var date in period1Dates)
{                          adapterBonus.InsertPeriod1Query(// insert to database)); }

etc.

Upvotes: 1

mmix
mmix

Reputation: 6278

All return a boolean specifying whether all elements satisfy a condition. What you need is a Where() to extract those you need.

Something like:

adapterBonus.InsertPeriod1Query(dates.Where(date => date >= startDatePeriod1 && date <= stopDatePeriod1));
adapterBonus.InsertPeriod2Query(dates.Where(date => date >= startDatePeriod2 && date <= stopDatePeriod2));

Upvotes: 1

Daniel Hilgarth
Daniel Hilgarth

Reputation: 174319

What you are doing, is the following:
Check whether ALL dates are in period one. If so, insert them into period 1 in the database. If not, check whether ALL dates are in period two and insert them into period 2 in the database.

You want to do the following:

foreach (var period1Date in dates.Where(date => date >= startDatePeriod1 && 
                                                date <= stopDatePeriod1))
{
    adapterBonus.InsertPeriod1Query(// insert period1Date to database));
}
foreach (var period2Date in dates.Where(date => date >= startDatePeriod2 && 
                                                date <= stopDatePeriod2))
{
    adapterBonus.InsertPeriod2Query(// insert period2Date to database));
}

BTW: I fixed an error in your second condition. It should be date <= stopDatePeriod2 instead of date >= stopDatePeriod2!

Upvotes: 2

Related Questions