tagged [foreach]

Update all objects in a collection using LINQ

Update all objects in a collection using LINQ Is there a way to do the following using LINQ? To clarify, I want to iterate through each object in a collection and then update a property on each object...

28 Apr at 11:30

How do you remove an array element in a foreach loop?

How do you remove an array element in a foreach loop? I want to loop through an array with `foreach` to check if a value exists. If the value does exist, I want to delete the element which contains it...

11 Sep at 22:2

foreach-ing through a listview and accessing subitems?

foreach-ing through a listview and accessing subitems? I'm having difficulty using a foreach statement with a WinForm ListView control. The following two code blocks demonstrates what I'm trying to do...

5 Apr at 14:27

Is if(items != null) superfluous before foreach(T item in items)?

Is if(items != null) superfluous before foreach(T item in items)? I often come across code like the following: Basically, the `if` condition ensures that `foreach` block will execute only if `items` i...

7 Oct at 19:33

c# getting a list from a field out of a list

c# getting a list from a field out of a list I'm sorry about the confusing title, but I didnt find a better way to explain my issue. I have a list of objects, `myList`, lets call them `MyObject`. the ...

10 Mar at 15:6

Modifying list inside foreach loop

Modifying list inside foreach loop I have a construction similar to this (but a more complicated): Now, I understand that its not possible to edit the c

9 Jun at 15:3

How to implement for-else and foreach-else in C# similar to Python's for-else and while-else?

How to implement for-else and foreach-else in C# similar to Python's for-else and while-else? Python's and loops include an optional clause which execute if the loop exits normally ( without a stateme...

7 Nov at 17:30

How to break nested foreach loop then go to parent foreach loop on c#

How to break nested foreach loop then go to parent foreach loop on c# I have the following code: ``` foreach(// Some condition here) { while (// Some condition here) { foreach (// Some conditi...

How to get linq `ForEach` statement to return data on the method call being made for each list object?

How to get linq `ForEach` statement to return data on the method call being made for each list object? I have a linq `ForEach` statement that calls a method for each `Report` object in the list. This ...

Parallel.ForEach slower than foreach

Parallel.ForEach slower than foreach Here is the code: ``` using (var context = new AventureWorksDataContext()) { IEnumerable _customerQuery = from c in context.Customers where ...

25 Jan at 16:55

Thread safety of yield return with Parallel.ForEach()

Thread safety of yield return with Parallel.ForEach() Consider the following code sample, which creates an enumerable collection of integers and processes it in parallel: ``` using System.Collections....

Parallel.Foreach + yield return?

Parallel.Foreach + yield return? I want to process something using parallel loop like this : Ok, it works fine. But How to do if I want the FillLogs method return an IEnumerable ? ``` public IEnumerab...

7 Dec at 09:38

Foreach loop XmlNodeList

Foreach loop XmlNodeList Currently I have the following code: Which doesn't w

7 Aug at 14:15

Getting "The connection does not support MultipleActiveResultSets" in a ForEach with async-await

Getting "The connection does not support MultipleActiveResultSets" in a ForEach with async-await I have the following code using Dapper.SimpleCRUD : ``` var test = new FallEnvironmentalCondition[] { ...

Why am I not getting a java.util.ConcurrentModificationException in this example?

Why am I not getting a java.util.ConcurrentModificationException in this example? Note: I am aware of the `Iterator#remove()` method. In the following code sample, I don't understand why the `List.rem...

Changing objects value in foreach loop?

Changing objects value in foreach loop? In one place i am using the list of string in that case the i am able to change the value of the string as code given below, But for object of class i am not ab...

30 Dec at 12:54

How to Convert all strings in List<string> to lower case using LINQ?

How to Convert all strings in List to lower case using LINQ? I saw a code snippet yesterday in one of the responses here on StackOverflow that intrigued me. It was something like this: I was hoping I ...

23 Oct at 18:54

Declaring a variable inside or outside an foreach loop: which is faster/better?

Declaring a variable inside or outside an foreach loop: which is faster/better? Which one of these is the faster/better one? This one: Or this one: ``` List list = new List(); foreach (string s in l) ...

22 Feb at 19:9

Difference between ThreadPool.QueueUserWorkItem and Parallel.ForEach?

Difference between ThreadPool.QueueUserWorkItem and Parallel.ForEach? What is the main difference between two of following approaches: ``` Clients objClien

Memory allocation when using foreach loops in C#

Memory allocation when using foreach loops in C# I know the basics on how foreach loops work in C# ([How do foreach loops work in C#](https://stackoverflow.com/questions/398982/how-do-foreach-loops-wo...

23 May at 12:32

Import-CSV and Foreach

Import-CSV and Foreach I've got a huge comma seperated CSV-list with IP-addresses of my network that I want to run queries against. Example of my CSV input: Etc.... When I run the following code to te...

2 Mar at 10:4

Is this use of Parallel.ForEach() thread safe?

Is this use of Parallel.ForEach() thread safe? Essentially, I am working with this: ``` var data = input.AsParallel(); List output = new List(); Parallel.ForEach(data, line => { String outputLine = ...

"Avoid allocations in compiler hot paths" Roslyn Coding Conventions

"Avoid allocations in compiler hot paths" Roslyn Coding Conventions I've been reading through the [Contributing Code](https://roslyn.codeplex.com/wikipage?title=How%20to%20Contribute&referringTitle=Ho...

20 Jun at 09:12

Nesting await in Parallel.ForEach

Nesting await in Parallel.ForEach In a metro app, I need to execute a number of WCF calls. There are a significant number of calls to be made, so I need to do them in a parallel loop. The problem is t...

Identifying last loop when using for each

Identifying last loop when using for each I want to do something different with the last loop iteration when performing 'foreach' on an object. I'm using Ruby but the same goes for C#, Java etc. The o...

1 Jul at 14:0