Quick Tip: 04. przeszukuj kolekcje z where zamiast foreach

LINQ udostępnia metodę where, która umożliwia przeszukiwanie kolekcji tak jak foreach.

Przykładowo, zamiast:

var polishCustomers = new List<Customer>();

foreach (var customer in customers)
{
if (customer.Country == "Poland")
polishCustomers.Add(customer);
}

możemy użyć

var polishCustomers = customers.Where(x => x.Country == "Poland");

czyli krótko, zwięźle i na temat :)

Oczywiście wcześniej musimy dodać using System.Linq;

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>