18

I want to create sequence numbers in asp.net mvc2..

Then number should start from { 0 to 1000}. I tried like following,

 var seq = Enumerable.Range(1, 1000);
        ViewData["OrderNo"] = seq;

In view:

 <%:Html.Hidden("OrderNo") %>
            <%:ViewData["OrderNo"] %>  

My result is

System.Linq.Enumerable+<RangeIterator>d__b8

But when getting value in view it is not working... How to generate sequential numbers?

PoliDev
  • 1,408
  • 9
  • 24
  • 45
  • Yes. this is good question u asked me. whenever user click the button it will generate the order as sequence numbers.. there is no limit.. I put 1000 for example.. – PoliDev Nov 13 '13 at 09:20
  • 1
    What "is not working"? Enumerable.Range definitely works. What did you expect to happen and what did actually happen? And if you didn't want to start from 1 why did you put 1 as the starting number? – Panagiotis Kanavos Nov 13 '13 at 09:21
  • that is not the issue. i want to start from 1 only. but my result comes in view empty value. – PoliDev Nov 13 '13 at 09:24
  • 1
    Pleas edit the question and add the specific problem. What result are you talking about and where does it come empty? Please add the relevant code. Also try to store an array instead of an IEnumerable by calling ToArray() after range – Panagiotis Kanavos Nov 13 '13 at 09:26
  • i updated my question. please check and help me. – PoliDev Nov 13 '13 at 09:34

2 Answers2

38

If you want to enumerate a sequence of numbers (IEnumerable<int>) from 0 to a variable end, then try

Enumerable.Range(0, ++end);

In explanation, to get a sequence of numbers from 0 to 1000, you want the sequence to start at 0 (remembering that there are 1001 numbers between 0 and 1000, inclusive).


If you want an unlimited linear series, you could write a function like

IEnumerable<int> Series(int k = 0, int n = 1, int c = 1)
{
    while (true)
    {
        yield return k;
        k = (c * k) + n;
    }
}

which you could use like

var ZeroTo1000 = Series().Take(1001);

If you want a function you can call repeatedly to generate incrementing numbers, perhaps you want somthing like.

using System.Threading;

private static int orderNumber = 0;

int Seq()
{
    return Interlocked.Increment(ref orderNumber);
}

When you call Seq() it will return the next order number and increment the counter.

jpaugh
  • 6,634
  • 4
  • 38
  • 90
Jodrell
  • 34,946
  • 5
  • 87
  • 124
  • if unlimited means how to do that? i want to start from 1 but i don't know the end value. – PoliDev Nov 13 '13 at 09:25
  • thank u so much. what is that interlocked. it is not supporting in vs 2010 mvc2? – PoliDev Nov 13 '13 at 09:57
  • @AngDei, it should work just fine. You'll need to reference `System.Threading` http://msdn.microsoft.com/en-us/library/dd78zt0c(v=vs.100).aspx – Jodrell Nov 13 '13 at 10:41
8

It's unclear what you mean or what you consider a failure, but the parameters of Enumerable.Range are start and count, not start and end.

If you want to generate numbers that start from 0 and end at 1000 you will have to write:

Enumerable.Range(0,1001);

If you have a different problem (eg the sequence doesn't persist between user calls, or from user to user or whatever), you will have to be specific.

Perhaps what you mean is that the IEnumerable you store in the ViewData doesn't get persisted as you expected in the View? That's because Range returns an object that implements IEnumerable and uses deferred execution to produce the requested values. Unless you force the enumeration with a for or a ToArray() the numbers will not be generated at all. Storing the object somewhere doesn't force an enumeration.

Panagiotis Kanavos
  • 120,703
  • 13
  • 188
  • 236