none
Project results of Multiple Groupby using System.Linq.Dynamic RRS feed

  • Question

  • Hi,

    I am trying to group following IEnumerable<Employee> by two of its properties State, Department.

    Employee[] empList = new Employee[6]; 
    empList[0] = new Employee() { Name = "CA", State = "A", Department = "xyz" }; 
    empList[1] = new Employee() { Name = "ZP", State = "B", Department = "xyz" }; 
    empList[2] = new Employee() { Name = "AC", State = "B", Department = "xyz" }; 
    empList[3] = new Employee() { Name = "AA", State = "A", Department = "xyz" }; 
    empList[4] = new Employee() { Name = "A2", State = "A", Department = "pqr" }; 
    empList[5] = new Employee() { Name = "BA", State = "B", Department = "pqr" }; 
     
    var empqueryable = empList.AsQueryable(); 
    var eq = empqueryable.GroupBy("new (State, Department)", "it").Select("new(it.Key as Key, it as Employees)");

    I dont understand how I should iterate through eq and get Key and  corresponding group of Employees list ? For instance the above should result in 4 groups each group containing the Key and List of elements grouped for that key value.

    Thanks


    • Edited by Mindspring Tuesday, September 18, 2012 11:41 PM added description
    Tuesday, September 18, 2012 11:35 PM

All replies

  • Hi Mindspring,

    Welcome to MSDN Forum.

    Please refer to the code below.

    class Program
        {
            static void Main(string[] args)
            {
                Employee[] empList = new Employee[6];
                empList[0] = new Employee() { Name = "CA", State = "A", Department = "xyz" };
                empList[1] = new Employee() { Name = "ZP", State = "B", Department = "xyz" };
                empList[2] = new Employee() { Name = "AC", State = "B", Department = "xyz" };
                empList[3] = new Employee() { Name = "AA", State = "A", Department = "xyz" };
                empList[4] = new Employee() { Name = "A2", State = "A", Department = "pqr" };
                empList[5] = new Employee() { Name = "BA", State = "B", Department = "pqr" };
    
                var empqueryable = empList.AsQueryable();           
    
                var query = empqueryable.GroupBy(e => new { e.State, e.Department });
                foreach (var v in query)
                {                
                    var list = v.OfType<Employee>();
                    for (int i = 0; i < list.Count(); i++)
                    {
                        Console.WriteLine(v.Key.State + " " + v.Key.Department + " " + list.ElementAt(i).Name);
                    }
                    
                }
                Console.Read();
            }
        }
    
        class Employee
        {
            public string Name { get; set; }
            public string State { get; set; }
            public string Department { get; set; }
        }

    Best Regards


    Allen Li [MSFT]
    MSDN Community Support | Feedback to us

    Thursday, September 20, 2012 2:24 AM
    Moderator
  • I am sorry I didnt explicitly mention ... I am trying to build a dynamic groupby using System.Linq.Dynamic.DynamicQueryable.

    Thanks

    Thursday, September 20, 2012 10:12 PM
  • In EF, we often use Entity SQL to build dynamic query, please look at the link below. This is the recommend way.

    http://msdn.microsoft.com/en-us/library/bb399764.aspx

    Friday, September 21, 2012 2:07 AM