How can I run a foreach loop in the immediate window of visual studio?



.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








2















I am trying to write values to a file using the Immediate Window in Visual Studio 2017.



I've got a variable called _myItems which is of type Dictionary<int, Dictionary<int, List<int>>>.



I did hit a breakpoint where this variable is in scope. I can run



?_myItems


in the window, and I'll get a list like:



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


To make sure that I can write a file in the immediate window, I ran:



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


Which did write the file, therefore I am sure I can write to a file from there.



I then tried the following:



?(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()); )


But I am getting the following error:




error CS1525: Invalid expression term 'foreach'




By searching around I came across this question but the accepted answer only state that you can do it.



How can I run this foreach loop in the Immediate Window to write the values to the file.



Note that I am aware that I should've done that inside my code, but I didn't think that I'll need the values later on. I was just checking the average. When I finished using the application I wrote, I decided to use those values. Given the time of preparing the values takes hours and hours, there's no possibility of just stopping the execution and re-write what I need in my code and go through the whole process again.



I am also aware that I can run



?_myItems[536][0] 


Then copying the values manually in the file. This process will also take a long time giving there's a lot of lists, and I would like to avoid it.



Is there any possibility to make this work in the immediate window?



Update:



I did as suggested in the answers:



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


I got the following error:




Evaluation of method System.Collections.Generic.List`1[System.Collections.Generic.Dictionary`2[System.Int32,System.Collections.Generic.List`1[System.Int32]]].ForEach() calls into native method Microsoft.Win32.Win32Native.GetFullPathName(). Evaluation of native methods in this context is not supported.




I even tried to just Debug.Print my values, for example like:



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


And I ended up getting the same error above. This time with the error of calling the native method:




System.Diagnostics.Debugger.IsLogging()




I searched for this error and according to this answer it's suggested to tick the Use Managed Compatibility Mode from debugging options. However, this option is grayed out in my case since I am already in a debugging session I assume.










share|improve this question
























  • Note, you don't need to use the ? prefix in the Immediate window.

    – Polyfun
    Nov 16 '18 at 13:51

















2















I am trying to write values to a file using the Immediate Window in Visual Studio 2017.



I've got a variable called _myItems which is of type Dictionary<int, Dictionary<int, List<int>>>.



I did hit a breakpoint where this variable is in scope. I can run



?_myItems


in the window, and I'll get a list like:



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


To make sure that I can write a file in the immediate window, I ran:



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


Which did write the file, therefore I am sure I can write to a file from there.



I then tried the following:



?(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()); )


But I am getting the following error:




error CS1525: Invalid expression term 'foreach'




By searching around I came across this question but the accepted answer only state that you can do it.



How can I run this foreach loop in the Immediate Window to write the values to the file.



Note that I am aware that I should've done that inside my code, but I didn't think that I'll need the values later on. I was just checking the average. When I finished using the application I wrote, I decided to use those values. Given the time of preparing the values takes hours and hours, there's no possibility of just stopping the execution and re-write what I need in my code and go through the whole process again.



I am also aware that I can run



?_myItems[536][0] 


Then copying the values manually in the file. This process will also take a long time giving there's a lot of lists, and I would like to avoid it.



Is there any possibility to make this work in the immediate window?



Update:



I did as suggested in the answers:



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


I got the following error:




Evaluation of method System.Collections.Generic.List`1[System.Collections.Generic.Dictionary`2[System.Int32,System.Collections.Generic.List`1[System.Int32]]].ForEach() calls into native method Microsoft.Win32.Win32Native.GetFullPathName(). Evaluation of native methods in this context is not supported.




I even tried to just Debug.Print my values, for example like:



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


And I ended up getting the same error above. This time with the error of calling the native method:




System.Diagnostics.Debugger.IsLogging()




I searched for this error and according to this answer it's suggested to tick the Use Managed Compatibility Mode from debugging options. However, this option is grayed out in my case since I am already in a debugging session I assume.










share|improve this question
























  • Note, you don't need to use the ? prefix in the Immediate window.

    – Polyfun
    Nov 16 '18 at 13:51













2












2








2








I am trying to write values to a file using the Immediate Window in Visual Studio 2017.



I've got a variable called _myItems which is of type Dictionary<int, Dictionary<int, List<int>>>.



I did hit a breakpoint where this variable is in scope. I can run



?_myItems


in the window, and I'll get a list like:



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


To make sure that I can write a file in the immediate window, I ran:



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


Which did write the file, therefore I am sure I can write to a file from there.



I then tried the following:



?(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()); )


But I am getting the following error:




error CS1525: Invalid expression term 'foreach'




By searching around I came across this question but the accepted answer only state that you can do it.



How can I run this foreach loop in the Immediate Window to write the values to the file.



Note that I am aware that I should've done that inside my code, but I didn't think that I'll need the values later on. I was just checking the average. When I finished using the application I wrote, I decided to use those values. Given the time of preparing the values takes hours and hours, there's no possibility of just stopping the execution and re-write what I need in my code and go through the whole process again.



I am also aware that I can run



?_myItems[536][0] 


Then copying the values manually in the file. This process will also take a long time giving there's a lot of lists, and I would like to avoid it.



Is there any possibility to make this work in the immediate window?



Update:



I did as suggested in the answers:



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


I got the following error:




Evaluation of method System.Collections.Generic.List`1[System.Collections.Generic.Dictionary`2[System.Int32,System.Collections.Generic.List`1[System.Int32]]].ForEach() calls into native method Microsoft.Win32.Win32Native.GetFullPathName(). Evaluation of native methods in this context is not supported.




I even tried to just Debug.Print my values, for example like:



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


And I ended up getting the same error above. This time with the error of calling the native method:




System.Diagnostics.Debugger.IsLogging()




I searched for this error and according to this answer it's suggested to tick the Use Managed Compatibility Mode from debugging options. However, this option is grayed out in my case since I am already in a debugging session I assume.










share|improve this question
















I am trying to write values to a file using the Immediate Window in Visual Studio 2017.



I've got a variable called _myItems which is of type Dictionary<int, Dictionary<int, List<int>>>.



I did hit a breakpoint where this variable is in scope. I can run



?_myItems


in the window, and I'll get a list like:



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


To make sure that I can write a file in the immediate window, I ran:



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


Which did write the file, therefore I am sure I can write to a file from there.



I then tried the following:



?(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()); )


But I am getting the following error:




error CS1525: Invalid expression term 'foreach'




By searching around I came across this question but the accepted answer only state that you can do it.



How can I run this foreach loop in the Immediate Window to write the values to the file.



Note that I am aware that I should've done that inside my code, but I didn't think that I'll need the values later on. I was just checking the average. When I finished using the application I wrote, I decided to use those values. Given the time of preparing the values takes hours and hours, there's no possibility of just stopping the execution and re-write what I need in my code and go through the whole process again.



I am also aware that I can run



?_myItems[536][0] 


Then copying the values manually in the file. This process will also take a long time giving there's a lot of lists, and I would like to avoid it.



Is there any possibility to make this work in the immediate window?



Update:



I did as suggested in the answers:



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


I got the following error:




Evaluation of method System.Collections.Generic.List`1[System.Collections.Generic.Dictionary`2[System.Int32,System.Collections.Generic.List`1[System.Int32]]].ForEach() calls into native method Microsoft.Win32.Win32Native.GetFullPathName(). Evaluation of native methods in this context is not supported.




I even tried to just Debug.Print my values, for example like:



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


And I ended up getting the same error above. This time with the error of calling the native method:




System.Diagnostics.Debugger.IsLogging()




I searched for this error and according to this answer it's suggested to tick the Use Managed Compatibility Mode from debugging options. However, this option is grayed out in my case since I am already in a debugging session I assume.







c# foreach visual-studio-2017 immediate-window






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 21 '18 at 8:51







Paul Karam

















asked Nov 16 '18 at 13:48









Paul KaramPaul Karam

2,48261935




2,48261935












  • Note, you don't need to use the ? prefix in the Immediate window.

    – Polyfun
    Nov 16 '18 at 13:51

















  • Note, you don't need to use the ? prefix in the Immediate window.

    – Polyfun
    Nov 16 '18 at 13:51
















Note, you don't need to use the ? prefix in the Immediate window.

– Polyfun
Nov 16 '18 at 13:51





Note, you don't need to use the ? prefix in the Immediate window.

– Polyfun
Nov 16 '18 at 13:51












2 Answers
2






active

oldest

votes


















0














As .ForEach does not exist on the Dictionary<TKey, TValue> type it only exists on the List<T> class.



Alternatively you can select the values of the dictionary, convert it into a list and walk through every value and write it into the file.



Adding to Helio Santos I would like to add:



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





share|improve this answer

























  • Thank you for your suggestion. I updated my question with an error that I got.

    – Paul Karam
    Nov 16 '18 at 15:22


















0














you can convert your foreach chain to a linq expression:



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





share|improve this answer

























  • .ForEach can't be used for a Dictionary<> as far as I know?

    – Paul Karam
    Nov 16 '18 at 14:34











  • @PaulKaram yes you are right as .ForEach only exists in List<T>

    – Kunal Mukherjee
    Nov 16 '18 at 14:36











Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53339142%2fhow-can-i-run-a-foreach-loop-in-the-immediate-window-of-visual-studio%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














As .ForEach does not exist on the Dictionary<TKey, TValue> type it only exists on the List<T> class.



Alternatively you can select the values of the dictionary, convert it into a list and walk through every value and write it into the file.



Adding to Helio Santos I would like to add:



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





share|improve this answer

























  • Thank you for your suggestion. I updated my question with an error that I got.

    – Paul Karam
    Nov 16 '18 at 15:22















0














As .ForEach does not exist on the Dictionary<TKey, TValue> type it only exists on the List<T> class.



Alternatively you can select the values of the dictionary, convert it into a list and walk through every value and write it into the file.



Adding to Helio Santos I would like to add:



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





share|improve this answer

























  • Thank you for your suggestion. I updated my question with an error that I got.

    – Paul Karam
    Nov 16 '18 at 15:22













0












0








0







As .ForEach does not exist on the Dictionary<TKey, TValue> type it only exists on the List<T> class.



Alternatively you can select the values of the dictionary, convert it into a list and walk through every value and write it into the file.



Adding to Helio Santos I would like to add:



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





share|improve this answer















As .ForEach does not exist on the Dictionary<TKey, TValue> type it only exists on the List<T> class.



Alternatively you can select the values of the dictionary, convert it into a list and walk through every value and write it into the file.



Adding to Helio Santos I would like to add:



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






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 16 '18 at 14:49

























answered Nov 16 '18 at 14:41









Kunal MukherjeeKunal Mukherjee

1,98121027




1,98121027












  • Thank you for your suggestion. I updated my question with an error that I got.

    – Paul Karam
    Nov 16 '18 at 15:22

















  • Thank you for your suggestion. I updated my question with an error that I got.

    – Paul Karam
    Nov 16 '18 at 15:22
















Thank you for your suggestion. I updated my question with an error that I got.

– Paul Karam
Nov 16 '18 at 15:22





Thank you for your suggestion. I updated my question with an error that I got.

– Paul Karam
Nov 16 '18 at 15:22













0














you can convert your foreach chain to a linq expression:



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





share|improve this answer

























  • .ForEach can't be used for a Dictionary<> as far as I know?

    – Paul Karam
    Nov 16 '18 at 14:34











  • @PaulKaram yes you are right as .ForEach only exists in List<T>

    – Kunal Mukherjee
    Nov 16 '18 at 14:36















0














you can convert your foreach chain to a linq expression:



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





share|improve this answer

























  • .ForEach can't be used for a Dictionary<> as far as I know?

    – Paul Karam
    Nov 16 '18 at 14:34











  • @PaulKaram yes you are right as .ForEach only exists in List<T>

    – Kunal Mukherjee
    Nov 16 '18 at 14:36













0












0








0







you can convert your foreach chain to a linq expression:



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





share|improve this answer















you can convert your foreach chain to a linq expression:



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






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 16 '18 at 17:59

























answered Nov 16 '18 at 14:03









Helio SantosHelio Santos

5,29932030




5,29932030












  • .ForEach can't be used for a Dictionary<> as far as I know?

    – Paul Karam
    Nov 16 '18 at 14:34











  • @PaulKaram yes you are right as .ForEach only exists in List<T>

    – Kunal Mukherjee
    Nov 16 '18 at 14:36

















  • .ForEach can't be used for a Dictionary<> as far as I know?

    – Paul Karam
    Nov 16 '18 at 14:34











  • @PaulKaram yes you are right as .ForEach only exists in List<T>

    – Kunal Mukherjee
    Nov 16 '18 at 14:36
















.ForEach can't be used for a Dictionary<> as far as I know?

– Paul Karam
Nov 16 '18 at 14:34





.ForEach can't be used for a Dictionary<> as far as I know?

– Paul Karam
Nov 16 '18 at 14:34













@PaulKaram yes you are right as .ForEach only exists in List<T>

– Kunal Mukherjee
Nov 16 '18 at 14:36





@PaulKaram yes you are right as .ForEach only exists in List<T>

– Kunal Mukherjee
Nov 16 '18 at 14:36

















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53339142%2fhow-can-i-run-a-foreach-loop-in-the-immediate-window-of-visual-studio%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

27

Top Tejano songwriter Luis Silva dead of heart attack at 64

Category:Rhetoric