dimanche 23 octobre 2016

Get object initialization syntax from C# list

I have a List of type employee.

public class Employee
    {
        public string Name { get; set; }
        public int Id { get; set; }
        public string Level { get; set; }
    }

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

Assume if this list has 10 items, from this list, I want to generate object initialization code. So basically I should get a string like this:

    "Employee e = new Employee{Name: "A", Id:1, Level:1};
    Employee e = new Employee{Name: "B", Id:2, Level:2};
    Employee e = new Employee{Name: "C", Id:3, Level:1};
Employee e = new Employee{Name: "D", Id:4, Level:6};
    Employee e = new Employee{Name: "E", Id:5, Level:2};
    Employee e = new Employee{Name: "F", Id:6, Level:5}
Employee e = new Employee{Name: "G", Id:7, Level:4};
    Employee e = new Employee{Name: "H", Id:8, Level:3};
    Employee e = new Employee{Name: "I", Id:9, Level:1};
Employee e = new Employee{Name: "J", Id:10, Level:1}";

The reason I am doing this is, I am getting list of records from database but it does not have a sort order (another property). I need to manually add the sort order to each item (hard code). So if I don't automate this, the only other option I will have would require me to create the object manually(instead of getting the values from database) and add SortOrder property to each object.





Aucun commentaire:

Enregistrer un commentaire