The conversion operators(continuation)

The ToDictionary() operator allows to create a Dictionary<TKey,TElement> based on the source sequence.

public static Dictionary<TKey, TSource> ToDictionary<TSource, TKey>(

    this IEnumerable<TSource> source,

    Func<TSource, TKey> keySelector);

 

public static Dictionary<TKey, TSource> ToDictionary<TSource, TKey>(

    this IEnumerable<TSource> source,

    Func<TSource, TKey> keySelector,

    IEqualityComparer<TKey> comparer);

 

public static Dictionary<TKey, TElement> ToDictionary<TSource, TKey,

TElement>(

    this IEnumerable<TSource> source,

    Func<TSource, TKey> keySelector,

    Func<TSource, TElement> elementSelector);

 

public static Dictionary<TKey, TElement> ToDictionary<TSource, TKey,

TElement>(

    this IEnumerable<TSource> source,

    Func<TSource, TKey> keySelector,

    Func<TSource, TElement> elementSelector,

    IEqualityComparer<TKey> comparer);

 

Example 3:

            var Customers = Data.Customers

                                .ToDictionary(key => key.Name, elem => elem.Orders);

 

            foreach (var item in Customers)

            {

                Trace.WriteLine(item.Key);

                foreach (var order in item.Value)

                {

                    Trace.WriteLine(order);

                }

            }

 

Output:

Paolo

3 – False – January – 1

5 – True – May – 2

Marco

10 – False – July – 1

20 – True – December – 3

James

20 – True – December – 3

20 – True – December – 3

20 – True – December – 3

Frank

20 – False – July – 5

 

The ToLookup() operator allows to create a Lookup<TKey, TElement> based on the source sequence.

 

public static Lookup<TKey, TSource> ToLookup<TSource, TKey>(

    this IEnumerable<TSource> source,

    Func<TSource, TKey> keySelector);

 

public static Lookup<TKey, TSource> ToLookup<TSource, TKey>(

    this IEnumerable<TSource> source,

    Func<TSource, TKey> keySelector,

    IEqualityComparer<TKey> comparer);

 

public static Lookup<TKey, TElement> ToLookup<TSource, TKey, TElement>(

    this IEnumerable<TSource> source,

    Func<TSource, TKey> keySelector,

    Func<TSource, TElement> elementSelector);

 

public static Lookup<TKey, TElement> ToLookup<TSource, TKey, TElement>(

    this IEnumerable<TSource> source,

    Func<TSource, TKey> keySelector,

    Func<TSource, TElement> elementSelector,

    IEqualityComparer<TKey> comparer);

 

public class Lookup<TKey, TElement> : IEnumerable<IGrouping<TKey, TElement>>

{

    public int Count { get; }

    public IEnumerable<TElement> this[TKey key] { get; }

    public bool Contains(TKey key);

    public IEnumerator<IGrouping<TKey, TElement>> GetEnumerator();

}

 

Example 4:

            var products = (from c in Data.Customers

                            from o in c.Orders

                            select o).ToLookup(p => p.IdProduct);

 

            Trace.WriteLine("There are :" + products.Count() + " products");

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

            foreach (var product in products)

            {

                Trace.WriteLine("ProductId:" + product.Key);

                foreach (var order in product)

                {

                    Trace.WriteLine(order);

                }

            }

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

            Trace.WriteLine("There are " + products[1].Count() + " products with id = 1");

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

            foreach (var order in products[1])

            {

                Trace.WriteLine("Order: " + order);

            }

 

Output:

There are :4 products

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

ProductId:1

3 – False – January – 1

10 – False – July – 1

ProductId:2

5 – True – May – 2

ProductId:3

20 – True – December – 3

20 – True – December – 3

20 – True – December – 3

20 – True – December – 3

ProductId:5

20 – False – July – 5

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

There are 2 products with id = 1

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

Order: 3 – False – January – 1

Order: 10 – False – July – 1

 

The OfType() operator filters the source sequence by the type of its elements.

public static IEnumerable<TResult> OfType<TResult>(this IEnumerable source);

 

Example 5:

 

            var orders = (from c in Data.Customers

                          from o in c.Orders

                          select o).OfType<Order>();

 

Output:

3 – False – January – 1

5 – True – May – 2

10 – False – July – 1

20 – True – December – 3

20 – True – December – 3

20 – True – December – 3

20 – True – December – 3

20 – False – July – 5

 

The Cast() operator casts elements of the source sequence to a given type:

public static IEnumerable<TResult> Cast<TResult>(this IEnumerable source);

 

Example 6:

 

            var ordersToString = (from c in Data.Customers

                                  from o in c.Orders

                                  select o).Cast<OrderBase>();

 

 

Output:

3 – False – January – 1

5 – True – May – 2

10 – False – July – 1

20 – True – December – 3

20 – True – December – 3

20 – True – December – 3

20 – True – December – 3

20 – False – July – 5

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