c# - 达到目标时如何重置计数器

标签 c# winforms

我有这段代码:

var cat = solutions
                .Descendants("Solution")
                .Select(x => new
                {
                    ID = (string)x.Element("ID"),
                    Properties = x.Elements("Property").Select(p => new
                    {
                        Name = (string) p.Element("Name"),
                        Value = (string) p.Element("Value"),

                        idx = i++
                    })
                    .Where(y => indexesToChoose.Contains(y.idx))
                    .OrderBy(z => indexesToChoose.FindIndex(p => p == z.idx))
                    .ToList()
                });

我只想让它数到 11,然后重置为 0 重新开始,例如它会读取 8,9,10,11,0,1 等。

最佳答案

最简单的方法是使用模数:

idx = i++ % 12

关于c# - 达到目标时如何重置计数器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31079658/

相关文章:

c# - 如何使用C#连接Mysql?

c# - 绑定(bind)不是可观察集合的 IEnumerable MVVM 属性的正确方法

c# - 如何在 C# 中捕获 Lua 异常

c# - 使用 NUnit 测试 Windows 窗体逻辑

c# - 如何创建具有可变数量 StackPanel 的 StackPanel 的 DataTemplate

C# Windows.Forms.WebBrowser 缩放

c# - Visual Studio 2008访问表单设计器时出错

c# - 括号还是引号?

c# - Form1_KeyDown 不工作

c# - 在 C# 中转换泛型接口(interface)和类