Use Linq to update items in a list without using a for loop!

class Program
    {
 
        public class MyEntity 
        {
            public int property1;
            public int property2;
            public int property3;
        }
 
        static void Main(string[] args)
        {
            List<MyEntity> entities = new List<MyEntity>();
            entities.Add(new MyEntity() { property1 = 1 });
            entities.Add(new MyEntity() { property1 = 1 });
            entities.Add(new MyEntity() { property1 = 1 });
            entities.Add(new MyEntity() { property1 = 1 });
 
            entities.ForEach(p => p.property1 = 2);
 
            Console.ReadLine();
        }
    }

Also, to make a select call without using for syntax:


List<MyEntity> filter = entities.Where(p => p.property1 == 2).ToList();

Where and Select without using for syntax:

list.Where(p => p.Property1 == value).Select(p => p.Property2Name).FirstOrDefault()

Comments

Popular posts from this blog

Enable Visual Studio to use more than 2GB of memory

Firefox and Chrome dark mode

Dealing with the morons who built Dell 7710 and RAID