Years Range. What am I doing wrong?

  • Follow


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
0
Reply shapper 1/17/2010 10:58:40 PM

shapper wrote:
> 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
0
Reply Peter 1/17/2010 11:31:56 PM


On Jan 17, 11:31=A0pm, Peter Duniho <no.peted.s...@no.nwlink.spam.com>
wrote:
> shapper wrote:
> > 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....
>
> 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. =A0One 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 =3D> i)

Thanks,
Miguel

0
Reply shapper 1/18/2010 12:30:24 AM

shapper wrote:
> 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.
0
Reply Harlan 1/18/2010 12:39:08 AM

shapper wrote:
> 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
0
Reply Peter 1/18/2010 12:54:30 AM

Peter Duniho wrote:
> [...]
>     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.  :)
0
Reply Peter 1/18/2010 1:05:25 AM

5 Replies
21947 Views

(page loaded in 0.002 seconds)

Similiar Articles:
















7/31/2012 7:07:52 AM


Reply: