sajju217
sajju217

Reputation: 467

Assign list of a class type to object type list

I have a list as below:

IList<Employee> emp = new List<Employee>
            {
                new Employee() {Id=1, Name = "A", Age = 20,  Address = "X"},
                new Employee() {Id=2, Name = "B", Age = 21,  Address = "Y"},
                new Employee() {Id=3, Name = "C", Age = 22,  Address = "Z"},

            };

and another list:IList<Object> emp2=new List<Object>(); If I want to assign emp to emp2 like:emp2=emp; how can I do this?

Upvotes: 0

Views: 1086

Answers (2)

phoog
phoog

Reputation: 43056

I would do this:

List<Employee> emp = //insert initialization code here
IList<object> emp2 = emp.OrderBy(e => e.WhicheverPropertyYouWantToSortBy).Cast<object>().ToList();

Upvotes: 1

nevets
nevets

Reputation: 4828

You can try this:

foreach (var employee in emp)
{
    emp2.Add(employee);
}

Upvotes: 1

Related Questions