none
Iterate list elements within a class and present into new class in C# RRS feed

  • Question

  • Iterate list elements within a class and present into new class in C#

    Here is my updated code sample and formatted.

    EmployeeClass object

    public class EmployeeClass
    {
    public int EId {get; set;}
    public int EName {get; set;}
    public List<Department> DeptList {get; set;}
    public List<Area> AreaList {get; set;}
    }
    
    public class Department
    {
        public int DepartmentID { get; set; }
        public string DepartmentName { get; set; }        
    }
    public class Area
    {
        public int AreaID { get; set; }
        public string AreaName { get; set; }        
    }

    The requirement here is, I want to return the output class sortedEmployeeClass in a such a way that it will return List of sortedEmployeeClass object by expanding Department and Area

    sortedEmployeeClass object extending EmployeeClass

    public class sortedEmployeeClass : EmployeeClass
    {
    public string DepartmentName {get; set;}
    public string  AreaName{get; set;}
    }

    The original object(EmployeeClass) has list of Employees with Department and Area as lists

    But I want to return my destination object (sortedEmployeeClass) as List of Employees with Department Names , Area Names as strings

    Hope this gives more visibility of what im looking for. Please let me know if you need more info.

    For example If Department list has HR, Safety etc... & Area list has Facility1 and Facility2 etc...
    My expected output would be....

    EId Ename DepartmentName AreaName
    1   Joe   HR             Facilty1
    1   Joe   Safety         Facilty1
    2   Jill  HR             Facilty2
    2   Jill  Safety         Facilty2

    • Edited by GeekPeek Wednesday, April 23, 2014 9:02 PM updated with additional information
    Wednesday, April 23, 2014 8:42 PM

Answers

  • I need the correct class structures.  The Select(x => x) method will enumerate through the List<Task>.  The code I posted is cloase to what you need.  In your case you would need two nested select methods.  One to enumerate through all employees, and then a second to enumerate through the employees tasks. 

    Note : The employeeClass.AsEnumerable() replaces the first enumeration.

    Thursday, April 24, 2014 11:50 AM

All replies

  • I made some changes in your class definitions.  It did not make much sense the an employee would below to more than on department and more than one area.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace ConsoleApplication1
    {
        class Program
        {
            public class EmployeeClass
            {
                public int EId { get; set; }
                public int EName { get; set; }
                public Department Dept { get; set; }
                public Area Area { get; set; }
            }
    
            public class Department
            {
                public int DepartmentID { get; set; }
                public string DepartmentName { get; set; }
            }
            public class Area
            {
                public int AreaID { get; set; }
                public string AreaName { get; set; }
            }
            public class SortedEmployeeClass : EmployeeClass
            {
                public string DepartmentName { get; set; }
                public string AreaName { get; set; }
            }
            static void Main(string[] args)
            {
                List<EmployeeClass> employeeClass = new List<EmployeeClass>();
    
                List<SortedEmployeeClass> sortedEmployeeClass = employeeClass.AsEnumerable()
                                                                .OrderBy(x => x.EId)
                                                                .Select(x => new SortedEmployeeClass()
                                                                {
                                                                    EId = x.EId,
                                                                    EName = x.EName,
                                                                    DepartmentName = x.Dept.DepartmentName,
                                                                    AreaName = x.Area.AreaName 
                                                                }).ToList();
            }
        }
        
    }
    
    


    jdweng

    Wednesday, April 23, 2014 10:33 PM
  • @jdweng, thanks for ur time / response. I understand your point that it doesn't male sense to have an employee with more than one department /area

    My bad example... :(

    But, lets assume an employee have list of tasks

    public List<Task> TasksList {get; set;}
    Basically I want to return list of employee + unique combination of tasks and facilities something like that.

    EId Ename TaskName AreaName
    1   Joe   Task1         Facilty1
    1   Joe  
    Task2         Facilty2
    2   Jill  Task1         Facility1
    2   Jill  Task2         Facilty2

    Thursday, April 24, 2014 11:13 AM
  • I need the correct class structures.  The Select(x => x) method will enumerate through the List<Task>.  The code I posted is cloase to what you need.  In your case you would need two nested select methods.  One to enumerate through all employees, and then a second to enumerate through the employees tasks. 

    Note : The employeeClass.AsEnumerable() replaces the first enumeration.

    Thursday, April 24, 2014 11:50 AM