Do you look for opportunities to use Linq?

Last updated by Brook Jeynes [SSW] 6 months ago.See history

Linq is a fantastic addition to .Net which lets you write clear and beautiful declarative code. Linq allows you to focus more on the what and less on the how .

You should look for opportunities to replace your existing code with Linq.

For example, replace your foreach loops with Linq.

var lucrativeCustomers = new List<Customer>();

foreach (var customer in Customers)
{
    if (customer.Orders.Count > 0)
    {
        lucrativeCustomers.Add(customer);
    }
}

Figure: Bad Example - imperative programming using a foreach loop

var lucrativeCustomers = Customers.Where(c => c.Orders.Count > 0).ToList();

Figure: Good Example - declarative programming using Linq

We open source. Powered by GitHub