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

Tutorial: Using Google Cloud Storage from C# and .NET

Late 2008 Macbook only giving 1.5 gb/s speed with 6 gb/s Intel SSD?

The ridiculous interview experience