Where operator

            1. What is the Where operator?

            2. What are the signatures of the Where operators?

            3. How to use the Where operator to restrict items in a collection by index?

To filter or to restrict items in a collection we can use the Where operator.

 

            var italians = from c in Data.Customers

                           where c.Country == Countries.Italy

                           select new { c.Name, c.City };

 

 

There are two versions of the Where operator:

 

public static IEnumerable<TSource> Where<TSource>(

    this IEnumerable<TSource> source,

    Func<TSource, bool> predicate);

 

public static IEnumerable<TSource> Where<TSource>(

    this IEnumerable<TSource> source,

    Func<TSource, int, bool> predicate);

 

 

To restrict items in a collection by index we can use second version of the Where operator.

 

            var start = 0;

            var end = 2;

 

            var italiansPage = Data.Customers.

                                Where((c, index) => c.Country == Countries.Italy && (index >= start && index <= end)).

                                Select(c => new { c.Name, c.City });

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s