Miscellaneous Operators

Concat 1

This sample uses Concat to create one sequence that contains each array's values, one after the other.

Code:

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

                var allNumbers = numbersA.Concat(numbersB);

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

Result:

Concat 2

This sample uses Concat to create one sequence that contains the names of all customers and products, including any duplicates.

Code:

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

                var customerNames = customers.Select(cust => cust.CompanyName);
                var productNames = products.Select(prod => prod.ProductName);

                var allNames = customerNames.Concat(productNames);

                Log.WriteLine("Customer and product names:");
                foreach (var n in allNames)
                {
                    Log.WriteLine(n);
                }
            }

Result:

EqualAll - 1

This sample uses SequenceEquals to see if two sequences match on all elements in the same order.

Code:

            public void Linq96()
            {
                var wordsA = new string[] { "cherry", "apple", "blueberry" };
                var wordsB = new string[] { "cherry", "apple", "blueberry" };

                bool match = wordsA.SequenceEqual(wordsB);

                Log.WriteLine("The sequences match: {0}", match);
            }

Result:

EqualAll - 2

This sample uses SequenceEqual to see if two sequences match on all elements in the same order.

Code:

            public void Linq97()
            {
                var wordsA = new string[] { "cherry", "apple", "blueberry" };
                var wordsB = new string[] { "apple", "blueberry", "cherry" };

                bool match = wordsA.SequenceEqual(wordsB);

                Log.WriteLine("The sequences match: {0}", match);
            }

Result: