Set operators

            1. What is the Distinct operator?

            2. What are the signatures of the Distinct operator?

            3. What is the Union operator?

            4. What are the signatures of the Union operator?

            5. What is the Intersect operator?

            6. What are the signatures of the Intersect operator?

            7. What is the Except operator?

            8. What are the signatures of the Except operator?

            The Distinct operator yields elements from the given sequence without duplicates.

There are two versions of the Distinct operator:

 

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

            public static IEnumerable<TSource> Distinct<TSource>( this IEnumerable<TSource> source,

            IEqualityComparer<TSource> comparer);

            By default the Distinct operator use the EqualityComparer<T>.Default which calls Equals() and GetHashCode() methods of the elements. So to avoid duplicates caused by default comparers we must override these methods.

Example 1:

 

        public static Customer[] Customers = new Customer[] {

            new Customer {Name = "Paolo", City = "Brescia", Country = Countries.Italy, Orders = new Order[] {

                new Order {Quantity = 3, IdProduct = 1, Shipped = false, Month = "January"},

                new Order {Quantity = 5, IdProduct = 2, Shipped = true, Month = "May"}}},

 

            new Customer {Name = "Marco", City = "Torino", Country = Countries.Italy, Orders =  new Order[] {

                new Order {Quantity = 10, IdProduct = 1, Shipped = false, Month = "July"},

                new Order {Quantity = 20, IdProduct = 3, Shipped = true, Month = "December"}}},

            new Customer {Name = "James", City = "Dallas", Country = Countries.USA, Orders =  new Order[] {

                new Order {Quantity = 5, IdProduct = 2, Shipped = false, Month = "July"},

                new Order {Quantity = 20, IdProduct = 3, Shipped = true, Month = "December"}}},

 

            new Customer {Name = "Frank", City = "Seattle", Country = Countries.USA, Orders =  new Order[] {

                new Order {Quantity = 20, IdProduct = 5, Shipped = false, Month = "July"}}},

            new Customer {Name = "Frank", City = "Sevastopol", Country = Countries.USA, Orders =  new Order[] {

                new Order {Quantity = 20, IdProduct = 5, Shipped = false, Month = "July"}}}};

             

            The Union operator yields sequences which were given as the parameters. If there are similar elements only one of the elements will be returned.

There are two versions of the Union operator:

 

public static IEnumerable<TSource> Union<TSource>(

    this IEnumerable<TSource> first,

    IEnumerable<TSource> second);

 

public static IEnumerable<TSource> Union<TSource>(

    this IEnumerable<TSource> first,

    IEnumerable<TSource> second,

    IEqualityComparer<TSource> comparer);

 

 

Example 2:

var unionexpr = Data.Customers[1].Orders.Union(Data.Customers[2].Orders);

 

Output:

 

10 – False – July – 1

20 – True – December – 3

5 – False – July – 2

 

            The Intersect operator yields elements which are similar for the given sequences.

            public static IEnumerable<TSource> Intersect<TSource>( this IEnumerable<TSource> first,

                IEnumerable<TSource> second);

            public static IEnumerable<TSource> Intersect<TSource>( this IEnumerable<TSource> first,

                IEnumerable<TSource> second,

                IEqualityComparer<TSource> comparer);

Example 3:

var intersectExpr = Data.Customers[1].Orders.Intersect(Data.Customers[2].Orders);

 

Output:

 

            20 – True – December – 3

            The Except operator yields elements from the first sequence which are not present in the second sequence.

            public static IEnumerable<TSource> Except<TSource>( this IEnumerable<TSource> first,

                IEnumerable<TSource> second);

            public static IEnumerable<TSource> Except<TSource>( this IEnumerable<TSource> first,

                IEnumerable<TSource> second,

                IEqualityComparer<TSource> comparer);

Example 4:

var exceptExpr = Data.Customers[1].Orders.Except(Data.Customers[2].Orders);

 

Output:

 

10 – False – July – 1

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