为什么不能从投影创建的整数变量递增?

时间:2019-01-16 18:14:35

标签: c# linq increment

我遇到了C#出乎意料的行为。幸运的是,我的单元测试捕获了意外的行为,但是我很惊讶,而且我不明白为什么它会如此表现。我用下面的代码重现了这个问题。

static void Main(string[] args)
{
    var initialCollection = new bool[] { false, true };
    var projectedCollection = initialCollection.Select(o => (initialObj: o, Counter: 0)).ToArray();

    for (int i = 0; i < 10; i++)
    {
        var objectFromProjection = projectedCollection.First(o => o.initialObj == (i % 2 == 0));
        Console.WriteLine($"For initial obj {objectFromProjection.initialObj}, counter is now {++objectFromProjection.Counter}");
    }

    Console.ReadKey();
}

由于我在投影之后添加了ToArray,所以我希望计数器变量不会每次都重新初始化为零。但是,对于循环for的每次迭代,它都会重新初始化为0。

以下是输出:

For initial obj True, counter is now 1
For initial obj False, counter is now 1
For initial obj True, counter is now 1
For initial obj False, counter is now 1
For initial obj True, counter is now 1
For initial obj False, counter is now 1
For initial obj True, counter is now 1
For initial obj False, counter is now 1
For initial obj True, counter is now 1
For initial obj False, counter is now 1

这就是我想做的:

For initial obj True, counter is now 1
For initial obj False, counter is now 1
For initial obj True, counter is now 2
For initial obj False, counter is now 2
For initial obj True, counter is now 3
For initial obj False, counter is now 3
For initial obj True, counter is now 4
For initial obj False, counter is now 4
For initial obj True, counter is now 5
For initial obj False, counter is now 5

为什么计数器会重置为零而不是保持递增的值?

2 个答案:

答案 0 :(得分:3)

因为您要处理的是元组。观察:

>>> from pywinauto.application import Application
>>> import pywinauto
>>> from pywinauto import findwindows
>>> app = pywinauto.Application().start(r"C:\Program Files\Keysight\ADS2020\bin\ads.exe")
>>> app = Application().connect(title=u'Advanced Design System 2020 (Main)', class_name='Qt5QWindowIcon')
>>> MenuItms = app.window_(title=u'Advanced Design System 2020 (Main)').MenuSelect("File->Exit")

Warning (from warnings module):
  File "__main__", line 1
DeprecationWarning: Method .window_() is deprecated, use .window() instead.

Warning (from warnings module):
  File "__main__", line 1
DeprecationWarning: Method .MenuSelect() is deprecated, use .menu_select() instead.
Traceback (most recent call last):
  File "<pyshell#5>", line 1, in <module>
    MenuItms = app.window_(title=u'Advanced Design System 2020 (Main)').MenuSelect("File->Exit")
  File "C:\Users\aakotkar\AppData\Local\Programs\Python\Python37\lib\site-packages\pywinauto\__init__.py", line 50, in wrap
    return method(*args, **kwargs)
  File "C:\Users\aakotkar\AppData\Local\Programs\Python\Python37\lib\site-packages\pywinauto\controls\hwndwrapper.py", line 1083, in menu_select
    self.menu_item(path, exact=exact).select()
  File "C:\Users\aakotkar\AppData\Local\Programs\Python\Python37\lib\site-packages\pywinauto\controls\hwndwrapper.py", line 1028, in menu_item
    raise RuntimeError("There is no menu.")
RuntimeError: There is no menu.

答案 1 :(得分:0)

Sub Transfer_Data()


Dim i As Long, j As Long

j = 1

For i = 1 To 6
    If Sheets("Sheet1").Cells(i, 1).Value <> "" Then
        Sheets("Sheet2").Cells(j, 1).Value = Sheets("Sheet1").Cells(i, 1).Value
        Sheets("Sheet2").Cells(j, 1).Font.Color = Sheets("Sheet1").Cells(i, 1).Font.Color
        j = j + 1
    End If

If j > 3 Then Exit For
Next i

End Sub

每次循环运行 var objectFromProjection = projectedCollection.First(o => o.initialObj == (i % 2 == 0)); Console.WriteLine($"For initial obj {objectFromProjection.initialObj}, counter is now {++objectFromProjection.Counter}"); 都很有意义,正在创建 ValueTuple ,并且值objectFromProjection始终为1,因为objectFromProjection返回时总是1总是一个元素。

我想这会为您澄清一些事情。

.First()