LINQ in C# - Non-Deferred Operator Examples - Conversion(ToArray, ToList, ToDictionary, ToLookup)

ToArray and ToList

  • The ToArray operator creates an array of type T from an input sequence of type T.
List<string> StudentNames = new List<string> { "Ram", "Raja", "Ravi"};

      //By default all the query opertors will return either IEnumerable/IQueryable
      IEnumerable<string> StudentsNameIEnumerable = StudentNames.Where(x => x.Length <= 4);

            string[] StudentsNameArray = StudentsNameIEnumerable.ToArray();
            List<string> StudentsNameList = StudentsNameIEnumerable.ToList();

ToDictionary - Type 1 - Example 1

class Customer
    {
        public int CustomerID { get; set; }
        public string CustomerType { get; set; }
        public string Name { get; set; }
    }

List<Customer> Customers = new List<Customer> {
                                    new Customer(){ CustomerID=1, Name="XYZ Private Limited", CustomerType="WholeSale" },
                                    new Customer(){ CustomerID=2, Name="ABC Private Limited", CustomerType="WholeSale" },
                                    new Customer(){ CustomerID=3, Name="MS Infotech", CustomerType="Retail" },
                                    new Customer(){ CustomerID=4, Name="Oracle" , CustomerType="WholeSale" },
                                    new Customer(){ CustomerID=5, Name="IBM Hardware Limited", CustomerType="Retail" },
                                 };

Dictionary<int, Customer> CustomerDictionary = Customers.ToDictionary(x => x.CustomerID);

ToDictionary - Type 2 with Custom comparer - Example 1

  • Same as like Group by Example with Custom Comparer, we can implement IEqualityComparer interface to compare the keys and for dictionary key must be unique, if not System.ArgumentException: 'An item with the same key has already been added.' exception will be thrown
class Customer
    {
        public int CustomerID { get; set; }
        public string CustomerType { get; set; }
        public string Name { get; set; }
    }

 class CustomerEqualityComparer : IEqualityComparer<int>
    {
        public bool Equals(int x, int y)
        {
            if (this.GetHashCode(x) == this.GetHashCode(y))
                return true;
            else
                return false;           
        }

        public int GetHashCode(int obj)
        {
            return obj;
        }
    }

List<Customer> Customers = new List<Customer> {
                                    new Customer(){ CustomerID=1, Name="XYZ Private Limited", CustomerType="WholeSale" },
                                    new Customer(){ CustomerID=2, Name="ABC Private Limited", CustomerType="WholeSale" },
                                    new Customer(){ CustomerID=3, Name="MS Infotech", CustomerType="Retail" },
                                    new Customer(){ CustomerID=4, Name="Oracle" , CustomerType="WholeSale" },
                                    new Customer(){ CustomerID=5, Name="IBM Hardware Limited", CustomerType="Retail" },
                                 };

Dictionary<int, Customer> CustomerDictionary = Customers.ToDictionary(x => x.CustomerID, new CustomerEqualityComparer() );

            foreach (var item in CustomerDictionary)
            {
                Console.WriteLine(item.Key);
                Console.WriteLine(item.Value.Name);

            }
//Output
1
XYZ Private Limited
2
ABC Private Limited
3
MS Infotech
4
Oracle
5
IBM Hardware Limited

ToDictionary - Type 3 with Custom Return Type- Example 1

  • This type is same as the first Type, with the small change that for the value type of the Dictionary, we specify a custom type
Dictionary<int, string> CustomerDictionary = Customers.ToDictionary(x => x.CustomerID, x=> x.Name);

ToDictionary - Type 4 Example 1

  • This type is a combination of Type 2 and Type 3.
Dictionary<int, string> CustomerDictionary = Customers.ToDictionary(x => x.CustomerID, x=> x.Name, new CustomerEqualityComparer());

ToLookup

  • Lookup works as a combination GroupBy and Dictionary
  • i.e. Same as like a Dictionary, it holds elements as a key,value pair, but in Dictionary each key will be with Single Value
  • But Lookup have Single key with Multiple Values, same as like a GroupBy option
  • All the 4 Types we saw above is applicable to ToLookUp duplicate Keys. 
//Type 1

List<Customer> Customers = new List<Customer> {
                                    new Customer(){ CustomerID=1, Name="XYZ Private Limited", CustomerType="WholeSale" },
                                    new Customer(){ CustomerID=2, Name="ABC Private Limited", CustomerType="WholeSale" },
                                    new Customer(){ CustomerID=2, Name="MS Infotech", CustomerType="Retail" },
                                    new Customer(){ CustomerID=4, Name="Oracle" , CustomerType="WholeSale" },
                                    new Customer(){ CustomerID=5, Name="IBM Hardware Limited", CustomerType="Retail" },
                                 };

ILookup<int, Customer> CustomerDictionary = Customers.ToLookup(x => x.CustomerID, new CustomerEqualityComparer());
           
            foreach (var item in CustomerDictionary)
            {
                Console.WriteLine(item.Key);

                foreach (var itemdetail in item)
                {
                    Console.WriteLine(itemdetail.Name);
                }
            }
//Output
1
XYZ Private Limited
2
ABC Private Limited
MS Infotech

4
Oracle
5
IBM Hardware Limited

//Type 2

ILookup<int, string> CustomerDictionary = Customers.ToLookup(x => x.CustomerID, x => x.Name);
           
            foreach (var item in CustomerDictionary)
            {
                Console.WriteLine(item.Key);

                foreach (var itemdetail in item)
                {
                    Console.WriteLine(itemdetail); //Customer Name
                }
            }

//Type 3

ILookup<int, string> CustomerDictionary = Customers.ToLookup(x => x.CustomerID, new CustomerEqualityComparer());
           
            foreach (var item in CustomerDictionary)
            {
                Console.WriteLine(item.Key);

                foreach (var itemdetail in item)
                {
                   Console.WriteLine(itemdetail.Name);
                }
            }

//Type 4

ILookup<int, string> CustomerDictionary = Customers.ToLookup(x => x.CustomerID, x=> x.Name, new CustomerEqualityComparer());
           
            foreach (var item in CustomerDictionary)
            {
                Console.WriteLine(item.Key);

                foreach (var itemdetail in item)
                {
                    Console.WriteLine(itemdetail); //Customer Name
                }
            }

No comments:

Post a Comment

Framework Fundamentals - String - Comparing Strings

In comparing two values, the .NET Framework differentiates the concepts of equality comparison and order comparison . Equality compariso...