Years Range. What am I doing wrong?

S

shapper

Hello,

I am need to create a range of year that goes from current year minus
70 to current year minus 16.
I want the recent years first. I am using:

Enumerable.Range(DateTime.UtcNow.AddYears(-16).Year,
DateTime.UtcNow.AddYears(-70).Year)

The problem is that I get 1994 to 3933.

What am I doing wrong?

Thanks,
Miguel
 
P

Peter Duniho

shapper said:
Hello,

I am need to create a range of year that goes from current year minus
70 to current year minus 16.
I want the recent years first. I am using:

Enumerable.Range(DateTime.UtcNow.AddYears(-16).Year,
DateTime.UtcNow.AddYears(-70).Year)

The problem is that I get 1994 to 3933.

What am I doing wrong?

Did you actually read the documentation for the Enumerable.Range()
method before you tried to use it?

http://msdn.microsoft.com/en-us/library/system.linq.enumerable.range.aspx

IMHO, the documentation makes it very clear what you've done wrong.
But, if after reading it you are still confused, please explain exactly
what about the documentation doesn't make sense to you. One particular
place to start would be to explain why it is you feel each value you are
passing to the method is justified given the documentation for that
parameter of the method.

Pete
 
S

shapper

Did you actually read the documentation for the Enumerable.Range()
method before you tried to use it?

http://msdn.microsoft.com/en-us/library/system.linq.enumerable.range....

IMHO, the documentation makes it very clear what you've done wrong.
But, if after reading it you are still confused, please explain exactly
what about the documentation doesn't make sense to you.  One particular
place to start would be to explain why it is you feel each value you are
passing to the method is justified given the documentation for that
parameter of the method.

Pete

Got it ... I have been using Enumeration always starting at 1 so I
ended up with the idea that it was start and end not start and count.
So I was completly convinced that the problem was with my DateTime
approach. Anyway it is working now:

Enumerable.Range(DateTime.UtcNow.AddYears(-70).Year,
55).OrderByDescending(i => i)

Thanks,
Miguel
 
H

Harlan Messinger

shapper said:
Hello,

I am need to create a range of year that goes from current year minus
70 to current year minus 16.
I want the recent years first. I am using:

Enumerable.Range(DateTime.UtcNow.AddYears(-16).Year,
DateTime.UtcNow.AddYears(-70).Year)

The problem is that I get 1994 to 3933.

You've asked for a range running from (2010 - 16) = 1994 and lasting
(2010 - 70) = 1940 years, until the year (1994 + 1940 - 1) = 3933.
 
P

Peter Duniho

shapper said:
Got it ... I have been using Enumeration always starting at 1 so I
ended up with the idea that it was start and end not start and count.
So I was completly convinced that the problem was with my DateTime
approach. Anyway it is working now:

Enumerable.Range(DateTime.UtcNow.AddYears(-70).Year,
55).OrderByDescending(i => i)

Of course, the above is a painfully clear example of overuse of LINQ.
Sorting a range of numbers by descending order, when the range is
_already_ sorted is ridiculous overhead. There's nothing the above
achieves that makes it superior to:

int year = DateTime.UtcNow.AddYears(-16),
yearLast = year - 55;

while (year > yearLast)
{
// do something

year--;
}

Even if for some reason you decided you needed the numbers represented
as an IEnumerable<int>, but didn't want them stored in some temporary
storage as you were enumerating them (an obvious contradiction with your
chosen implementation, but let's say that constraint exists just for the
sake of discussion), then something like this would make more sense:

IEnumerable<int> Range(int start, int count)
{
if (count >= 0)
{
return Enumerable.Range(start, count);
}

if (start + count + 1 > start)
{
throw new ArgumentOutOfRangeException("when count is negative,
start + count + 1 cannot be less than Int32.MinValue");
}

while (count < 0)
{
yield return start++;

count++;
}
}

Hope that helps.

Pete
 
P

Peter Duniho

Peter said:
[...]
while (count < 0)
{
yield return start++;

count++;
}

The above should, of course, read:

while (count < 0)
{
yield return start--;

count++;
}

I'm guessing everyone can figure that out though. :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top