The conversion operators

  1. Why do we need the conversion operators?

2. What is the AsEnumerable operator?

3. What is the signature of the AsEnumerable operator?

4. What are the ToArray(), ToList() operators?

5. What are the signatures of the ToArray(), ToList() operators?

6. What is the ToDictionary() operator?

7. What is the signature of the ToDictionary() operator?

8. What is the ToLookup() operator?

9. What is the signature of the ToLookup () operator?

10. What is the OfType() operator?

11. What is the signature of the OfType() operator?

12. What is the Cast() operator?

13. What is the signature of the Cast() operator?

We can implement our own operators, but sometimes we need to call standard LINQ operators, so to get opportunity to call standard operators we can call the AsEnumerable() operator:

public static IEnumerable<TSource> AsEnumerable<TSource>( this IEnumerable<TSource> source );

Example 1:

    public sealed class Customers : List<Customer>

    {

        public Customers(IEnumerable<Customer> items) : base(items) { }

    }

 

    public static class CustomersOperators

    {

        public static IEnumerable<Customer> Where(this Customers items, Func<Customer, bool> filter)

        {

            return items;

        }

    }

            var customers = new Customers(Data.Customers);

 

            var specificWhereExpr = from c in customers

                                    where c.Country == Countries.Italy

                                    select c;

 

            foreach (var item in specificWhereExpr)

            {

                Trace.WriteLine(item);

            }

 

            Trace.WriteLine("-========================================-");

 

            var genericWhereExpr = from c in customers.AsEnumerable()

                                   where c.Country == Countries.Italy

                                   select c;

 

            foreach (var item in genericWhereExpr)

            {

                Trace.WriteLine(item);

            }

Output:

 

Paolo – Brescia – Italy

Marco – Torino – Italy

James – Dallas – USA

Frank – Seattle – USA

-========================================-

Paolo – Brescia – Italy

Marco – Torino – Italy

To avoid unnecessary evaluation we can use ToArray() or ToList() operators. Using those operators we can get a snapshot of a source array.

 

public static TSource[] ToArray<TSource>( this IEnumerable<TSource> source );

public static List<TSource> ToList<TSource>( this IEnumerable<TSource> source );

 

Example 2:

            var productGreater30 = (from p in Data.Products

                                   where p.Price > 30

                                   select p).ToArray();

 

            foreach (var item in productGreater30)

            {

                Trace.WriteLine(item);

            }

 

            var OrdersByCustomer = from c in Data.Customers

                                   from o in c.Orders

                                   join p in productGreater30 on o.IdProduct equals p.IdProduct

                                   select new { c.Name, Total = p.Price * o.Quantity };

 

            foreach (var item in OrdersByCustomer)

            {

                Trace.WriteLine(item);

            }

Output:

4 – 40

5 – 50

6 – 60

{ Name = Frank, Total = 1000 }

 

 

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