C# 生成数字序列
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19949640/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Generate a sequence of numbers
提问by PoliDev
I want to create sequence numbers in asp.net mvc2..
我想在asp.net mvc2中创建序列号..
Then number should start from { 0 to 1000}
. I tried like following,
那么数字应该从{ 0 to 1000}
. 我试过如下,
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?
但是当获得价值时它不起作用......如何生成序列号?
采纳答案by Jodrell
If you want to enumerate a sequence of numbers (IEnumerable<int>
) from 0
to a variable end
, then try
如果你想枚举一个数字序列 ( IEnumerable<int>
)0
到一个变量end
,然后尝试
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).
作为解释,要获得从 0 到 1000 的数字序列,您希望该序列从 0 开始(请记住,在 0 和 1000 之间有 1001 个数字,包括 0 和 1000)。
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.
当您调用Seq()
它时,它将返回下一个订单号并增加计数器。
回答by Panagiotis Kanavos
It's unclear what you mean or what you consider a failure, but the parameters of Enumerable.Rangeare start
and count
, not start
and end
.
目前尚不清楚您的意思或您认为失败的原因,但Enumerable.Range的参数是start
and count
,而不是start
and end
。
If you want to generate numbers that start from 0 and end at 1000 you will have to write:
如果要生成从 0 开始到 1000 结束的数字,则必须编写:
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.
也许您的意思是您存储在 ViewData 中的 IEnumerable 没有按照您在 View 中的预期持久化?这是因为 Range 返回一个实现 IEnumerable 的对象并使用延迟执行来生成请求的值。除非您使用 afor
或 ToArray()强制枚举,否则根本不会生成数字。将对象存储在某处不会强制进行枚举。