问题描述
我想在asp.net mvc2中创建序列号.
I want to create sequence numbers in asp.net mvc2..
然后数字应从 {0到1000}
开头.我想像以下那样
Then number should start from { 0 to 1000}
. I tried like following,
var seq = Enumerable.Range(1, 1000);
ViewData["OrderNo"] = seq;
视图中:
<%:Html.Hidden("OrderNo") %>
<%:ViewData["OrderNo"] %>
我的结果是
System.Linq.Enumerable+<RangeIterator>d__b8
但是在获取价值时,它不起作用...如何生成序号?
But when getting value in view it is not working... How to generate sequential numbers?
推荐答案
如果要枚举从 0
到a的数字序列( IEnumerable< int>
)变量 end
,然后尝试
If you want to enumerate a sequence of numbers (IEnumerable<int>
) from 0
to a variable end
, then try
Enumerable.Range(0, ++end);
在解释中,要获得一个从0到1000的数字序列,您希望该序列从0开始(请记住,在0到1000之间(包括0和1000),其中包括1001个数字).
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;
}
}
您可以像
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);
}
调用 Seq()
时,它将返回下一个订单号并增加计数器.
When you call Seq()
it will return the next order number and increment the counter.
这篇关于生成数字序列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!