• How to combine lists of dictionary in LinQ

    Posted on July 31, 2012 by in C#, Dotnet

    In this example, I am going create bunch of products with some sample data. Every product belongs to a subcategory. Products belonging to the same subcategory are added to a list and that list is added to the dictionary with SubCategoryName as the key. The values collection of the dictionary are product lists. We use SelectMany function to combine all those lists.

    As you can see below, I have created a simple product class for our example.

    Product.cs:

    public class Product
    {
        public int ProductID;
        public string ProductName;
        public string ProductSubCategoryName;
    }
    

    I have created a helper method that would exactly that.

    public static void MergeListsValuesOfDictionary()
    {
        Dictionary<string, List<Product>> productsHash = new Dictionary<string, List<Product>>();
                    
        //populate some sample data
        //SubCategory: "Brakes"
        List<Product> products = new List<Product>();
        Product product = new Product();
        product.ProductID = 680;
        product.ProductName = "HL Road Frame - Black, 58";
        product.ProductSubCategoryName = "Brakes";
        products.Add(product);
    
        product = new Product();
        product.ProductID = 907;
        product.ProductName = "Rear Brakes";
        product.ProductSubCategoryName = "Brakes";
        products.Add(product);
    
        productsHash.Add("Brakes", products);
    
        //SubCategory: "Road Bikes"
        products = new List<Product>();
        product = new Product();
        product.ProductID = 706;
        product.ProductName = "HL Road Frame - Red, 58";
        product.ProductSubCategoryName = "Road Bikes";
        products.Add(product);
    
        product = new Product();
        product.ProductID = 749;
        product.ProductName = "Road-150 Red, 62";
        product.ProductSubCategoryName = "Road Bikes";
        products.Add(product);
    
        productsHash.Add("Road Bikes", products);
    
        //SubCategory: "Touring Bikes"
        products = new List<Product>();
        product = new Product();
        product.ProductID = 953;
        product.ProductName = "Touring-2000 Blue, 60";
        product.ProductSubCategoryName = "Touring Bikes";
        products.Add(product);
    
        product = new Product();
        product.ProductID = 954;
        product.ProductName = "Touring-1000 Yellow, 46";
        product.ProductSubCategoryName = "Touring Bikes";
        products.Add(product);
    
        productsHash.Add("Touring Bikes", products);
    
        List<Product> mergedProductsList = productsHash.Values.SelectMany(x => x).ToList();
    
        //iterate through product groups and print product attributes
        foreach (Product prd in mergedProductsList)
        {
            Console.WriteLine("\tProduct ID:                " + prd.ProductID);
            Console.WriteLine("\tProduct Name:              " + prd.ProductName);
            Console.WriteLine("\tProduct Sub CategoryName:  " + prd.ProductSubCategoryName);
            Console.WriteLine("\t----------------------------------------");
        }
    }
    

    Output: 

    Be Sociable, Share!
      Post Tagged with ,

    Written by

    Software architect with over 10 years of proven experience in designing & developing n-tier and web based software applications, for Finance, Telecommunication, Manufacturing, Internet and other Commercial industries. He believes that success depends on one's ability to integrate multiple technologies to solve a simple as well as complicated problem.

    View all articles by

    Email : [email protected]

    Leave a Reply