convert foreach to linq c# code example

Example 1: linq foreach c#

sequence.Where(x => x.Name.ToString().Equals("Apple"))
    .ToList()
    .ForEach( x =>
     {
       if(someCondition)
       {
         // Do some stuff here.
       }  
     });

Example 2: foreach c# linq example

items.ToList().ForEach(i => i.DoStuff());

Example 3: convert foreach to linq c#

foreach (var customer in customers)
            {
                customer.Email = !string.IsNullOrEmpty(customer.Name) && !string.IsNullOrEmpty(customer.Last) ? $"{customer.Name}.{customer.Last}{customer.Email.Substring(customer.Email.IndexOf("@"))}" : customer.Email;
            }

Tags:

Misc Example