【问题标题】:How can I run a foreach loop in the immediate window of visual studio?如何在 Visual Studio 的即时窗口中运行 foreach 循环?
【发布时间】:2018-11-16 13:48:53
【问题描述】:

我正在尝试使用 Visual Studio 2017 中的 Immediate Window 将值写入文件。

我有一个名为_myItems 的变量,其类型为Dictionary<int, Dictionary<int, List<int>>>

我确实遇到了breakpoint,这个变量在范围内。我可以跑

?_myItems

在窗口中,我会得到如下列表:

Count = 9
    [0]: {[536], System.Collections.Generic.Dictionary`2[System.Int32,System.Collections.Generic.List`1[System.Int32]]]}
    [1]... omitted for clearance
    ...    omitted for clearance
    ...    omitted for clearance
    [8]... omitted for clearance

为了确保我可以在即时窗口中写入文件,我运行了:

File.WriteAllText("test.txt", "testing purposes");

哪个确实写入了文件,因此我确信我可以从那里写入文件。

然后我尝试了以下方法:

?(foreach (KeyValuePair<int, Dictionary<int, List<int>>> pair in _myItems) { foreach (KeyValuePair<int, List<int>> valuePair in pair.Value) { foreach (int numberToWrite in valuePair.Value) { File.AppendAllText("test.txt",numberToWrite.ToString()); } }})

但我收到以下错误:

错误 CS1525:无效的表达式术语“foreach”

通过四处搜索,我发现了this question,但接受的答案仅表明您可以做到。

如何在即时窗口中运行 foreach 循环以将值写入文件。

请注意,我知道我应该在我的代码中这样做,但我认为我以后不需要这些值。我只是检查平均值。当我完成使用我编写的应用程序时,我决定使用这些值。鉴于准备值需要数小时和数小时,不可能只是停止执行并在我的代码中重新编写我需要的内容并再次完成整个过程。

我也知道我可以跑

?_myItems[536][0] 

然后在文件中手动复制值。这个过程也需要很长时间,因为列表很多,我想避免它。

是否有可能在即时窗口中进行这项工作?

更新

我按照答案中的建议做了:

_myItems.Select(x => x.Value)
                    .ToList()
                    .ForEach(pair => pair
                                     .ToList()
                                     .ForEach(valuePair => valuePair
                                                           .Value
                                                           .ForEach(numberToWrite => File.AppendAllText("test.txt", numberToWrite.ToString()))))

我收到以下错误:

评估方法 System.Collections.Generic.List`1[System.Collections.Generic.Dictionary`2[System.Int32,System.Collections.Generic.List`1[System.Int32]]].ForEach()调用本机方法 Microsoft.Win32.Win32Native.GetFullPathName()。不支持在此上下文中评估本机方法。

我什至尝试只使用Debug.Print 我的价值观,例如:

_myItems.ToList().ForEach(pair => System.Diagnostics.Debug.Print(pair.Key));

我最终得到了同样的错误。这次是调用原生方法的错误:

System.Diagnostics.Debugger.IsLogging()

我搜索了这个错误,根据this answer,建议在调试选项中勾选使用托管兼容模式。但是,在我的情况下,这个选项是灰色的,因为我已经在我假设的调试会话中。

【问题讨论】:

  • 注意,你不需要使用 ?立即窗口中的前缀。

标签: c# foreach visual-studio-2017 immediate-window


【解决方案1】:

由于.ForEach 不存在于Dictionary&lt;TKey, TValue&gt; 类型中,它只存在于List&lt;T&gt; 类中。

或者,您可以选择字典的值,将其转换为列表并遍历每个值并将其写入文件。

添加到Helio Santos 我要添加:

_myItems.Select(x => x.Value).ToList().ForEach(pair => pair.ForEach(intList => intList.ForEach(numberToWrite => File.AppendAllText("test.txt", numberToWrite.ToString()))));

【讨论】:

  • 感谢您的建议。我用我得到的错误更新了我的问题。
【解决方案2】:

您可以将您的 foreach 链转换为 linq 表达式:

_myItems.Select(x => x.Value).ToList().ForEach(pair => pair.ForEach(intList => intList.ForEach(numberToWrite => File.AppendAllText("test.txt", numberToWrite.ToString()))));

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-01-17
    • 1970-01-01
    • 1970-01-01
    • 2021-10-23
    • 1970-01-01
    • 2014-11-07
    • 1970-01-01
    相关资源
    最近更新 更多