Adding a field of an object to list in C# when list

Alexander Westman

So I have a list of objects (AllCompanies), and all the objects in the list inherit from the same class (Company). And I also have a field in the class they all inherit from which is another class entirely (Employees). I want to create a list of every Employee currently active, and I have every company in a list of objects. As you can see below I have tried to simply add the Employee's field to the list, but I get an error because I am trying to convert an object to a list of employees.

public class Main()
{
    List<List<Employee>> AllEmployees = new List<List<Employee>>();

    List<object> AllCompanies = new List<object>() { new SmallCompany(), new LargeCompany(), new SmallCompany() };

    AllCompanies.ForEach(delegate(object o)
    {
        // - Here's the problem.
        AllEmployees.Add(o.GetType().GetField("Employees").GetValue(o));
        // - Here's the problem.
    });
}

class Employee
{
    public string Name;
    public int Age;
    public Employee(string name, int age)
    {
        Name = name;
        Age = age;
    }
}

class Company
{
    public List<Employee> Employees = new List<Employee>();
}

class SmallCompany : Company
{
    public SmallCompany(){}
}

class LargeCompany : Company
{
    public LargeCompany(){}
}

How can I do this most efficiently?

Servy

You shouldn't type the list as a list of object types. The objects are all Company objects, and the list should reflect that.

List<Company> AllCompanies = new List<Company>() 
{
        new SmallCompany(), 
        new LargeCompany(), 
        new SmallCompany() 
};

That will remove the need for any of that awful reflection code that will just make life hard.

Once you have that projecting each item into a new item given a selector is easy enough to do:

var allEmployees = AllCompanies.Select(company => company.Employees);

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Adding object to a linked list c++

From Dev

Adding object to a list with a for loop?

From Dev

c# convert object with List field to List using LINQ

From Dev

Sorting List By Object Field

From Dev

C# find object in list using a object field

From Dev

Adding subclass object to superclass list

From Dev

Adding the same object to context and a list

From Dev

OOP parsing and adding object to list

From Dev

Adding new object to List is not working

From Dev

Adding an object to the end of the circular list

From Dev

python adding a class object to a list

From Dev

Adding Integer and String to List<Object>

From Dev

C# Do something while adding object to list

From Dev

Pymongo: Select field into list without adding field name to list

From Dev

Filter list by properties of a field of the list object

From Dev

Index was outside the bounds of the array when adding class to list C#

From Dev

IP address exception when adding to a list in C#

From Dev

Issue when adding element into sorted linked list C++

From Dev

C# List Elements being overwritten when adding a new element

From Dev

EXC_BAD ACCESS when adding node to linked list in C

From Dev

Adding to a list when a variable is repeated

From Dev

Rename a field in each object of a list

From Dev

Rename a field in each object of a list

From Dev

Sitecore adding field labels to name value list

From Dev

C# - Adding data to list inside list

From Dev

C# Nested List not adding a new list

From Dev

c# - Index of object in a list when a variable in the object changes

From Dev

Memory overhead when adding an object to a List directly vs. object creation

From Dev

Adding to the back of linked list C

Related Related

HotTag

Archive