Construct instance for nested class : Anonymous « LINQ « C# / CSharp Tutorial






using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

    class Employee
    {
        public string Name { get; set; }
        public decimal Salary { get; set; }
    }

    class Department
    {
        public string Name { get; set; }

        List<Employee> employees = new List<Employee>();

        public IList<Employee> Employees
        {
            get { return employees; }
        }
    }

    class Company
    {
        public string Name { get; set; }

        List<Department> departments = new List<Department>();

        public IList<Department> Departments
        {
            get { return departments; }
        }
    }

    class SalaryReport
    {
        static void Main()
        {
            var company = new Company
            {
                Name = "A",
                Departments =
                {
                    new Department 
                    { 
                        Name = "Development", 
                        Employees = 
                        {
                            new Employee { Name = "T", Salary = 75000m },
                            new Employee { Name = "D", Salary = 45000m },
                            new Employee { Name = "M", Salary = 150000m }
                        }
                    },
                    new Department 
                    { 
                        Name = "Marketing", 
                        Employees = 
                        {
                            new Employee { Name = "Z", Salary = 200000m },
                            new Employee { Name = "X", Salary = 120000m }
                        }
                    }
                }
            };

            var query = company.Departments
                               .Select(dept => new { dept.Name, Cost = dept.Employees.Sum(person => person.Salary) })
                               .OrderByDescending(deptWithCost => deptWithCost.Cost);

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








22.12.Anonymous
22.12.1.Anonymous Method
22.12.2.Anonymous Type
22.12.3.Anonymous Type In Array
22.12.4.Anonymous Types
22.12.5.Object Initializer
22.12.6.Create new object from query
22.12.7.Simple Anonymous Methods for calculating the mean value
22.12.8.Simple Anonymous Methods for printing out reverse numbers
22.12.9.Simple Anonymous Methods for printing the square root
22.12.10.Return From Anonymous Method
22.12.11.Using Anonymous Methods with Find
22.12.12.Array of anonymous types
22.12.13.Construct instance for nested class
22.12.14.Implicit Local Variables with Anonymous Types