Set Operators

Distinct - 1

This sample uses Distinct to remove duplicate elements in a sequence of factors of 300.

Code:

            public void Linq46()
            {
                int[] factorsOf300 = { 2, 2, 3, 5, 5 };

                var uniqueFactors = factorsOf300.Distinct();

                Log.WriteLine("Prime factors of 300:");
                foreach (var f in uniqueFactors)
                {
                    Log.WriteLine(f);
                }
            }

Result:

Distinct - 2

This sample uses Distinct to find the unique Category names.

Code:

            public void Linq47()
            {
                List products = GetProductList();

                var categoryNames = (products.Select(prod => prod.Category)).Distinct();

                Log.WriteLine("Category names:");
                foreach (var n in categoryNames)
                {
                    Log.WriteLine(n);
                }
            }

Result:

Union 1

This sample uses Union to create one sequence that contains the unique values from both arrays.

Code:

            public void Linq48()
            {
                int[] numbersA = { 0, 2, 4, 5, 6, 8, 9 };
                int[] numbersB = { 1, 3, 5, 7, 8 };

                var uniqueNumbers = numbersA.Union(numbersB);

                Log.WriteLine("Unique numbers from both arrays:");
                foreach (var n in uniqueNumbers)
                {
                    Log.WriteLine(n);
                }
            }

Result:

Union 2

This sample uses the Union method to create one sequence that contains the unique first letter from both product and customer names. Union is only available through method syntax.

Code:

            public void Linq49()
            {
                List products = GetProductList();
                List customers = GetCustomerList();

                var productFirstChars = products.Select(prod => prod.ProductName[0]);
                var customerFirstChars = customers.Select(cust => cust.CompanyName[0]);

                var uniqueFirstChars = productFirstChars.Union(customerFirstChars);

                Log.WriteLine("Unique first letters from Product names and Customer names:");
                foreach (var ch in uniqueFirstChars)
                {
                    Log.WriteLine(ch);
                }
            }

Result:

Intersect - 1

This sample uses Intersect to create one sequence that contains the common values shared by both arrays.

Code:

            public void Linq50()
            {
                int[] numbersA = { 0, 2, 4, 5, 6, 8, 9 };
                int[] numbersB = { 1, 3, 5, 7, 8 };

                var commonNumbers = numbersA.Intersect(numbersB);

                Log.WriteLine("Common numbers shared by both arrays:");
                foreach (var n in commonNumbers)
                {
                    Log.WriteLine(n);
                }
            }

Result:

Intersect - 2

This sample uses Intersect to create one sequence that contains the common first letter from both product and customer names.

Code:

            public void Linq51()
            {
                List products = GetProductList();
                List customers = GetCustomerList();

                var productFirstChars = products.Select(prod => prod.ProductName[0]);
                var customerFirstChars = customers.Select(cust => cust.CompanyName[0]);

                var commonFirstChars = productFirstChars.Intersect(customerFirstChars);

                Log.WriteLine("Common first letters from Product names and Customer names:");
                foreach (var ch in commonFirstChars)
                {
                    Log.WriteLine(ch);
                }
            }

Result:

Except - 1

This sample uses Except to create a sequence that contains the values from numbersAthat are not also in numbersB.

Code:

            public void Linq52()
            {
                int[] numbersA = { 0, 2, 4, 5, 6, 8, 9 };
                int[] numbersB = { 1, 3, 5, 7, 8 };

                IEnumerable aOnlyNumbers = numbersA.Except(numbersB);

                Log.WriteLine("Numbers in first array but not second array:");
                foreach (var n in aOnlyNumbers)
                {
                    Log.WriteLine(n);
                }
            }

Result:

Except 2

This sample uses Except to create one sequence that contains the first letters of product names that are not also first letters of customer names.

Code:

            public void Linq53()
            {
                List products = GetProductList();
                List customers = GetCustomerList();

                var productFirstChars = products.Select(prod => prod.ProductName[0]);
                var customerFirstChars = customers.Select(cust => cust.CompanyName[0]);

                var productOnlyFirstChars = productFirstChars.Except(customerFirstChars);

                Log.WriteLine("First letters from Product names, but not from Customer names:");
                foreach (var ch in productOnlyFirstChars)
                {
                    Log.WriteLine(ch);
                }
            }

Result: