简体   繁体   中英

Cannot implicitly convert type 'System.Collections.Generic.List<Data.emp>' to 'System.Collections.Generic.List<User.Employee>'

been stuck with this error for a while now. basically i have 2 C# class library HeldDeskBusinessData and HelpDeskBusinessUser (shortened for the title)

from HelpDeskBusinessData

     public List<Employee> GetAll()
    {
        HelpDeskDBEntities dbContext = new HelpDeskDBEntities();
        List<Employee> employees = dbContext.Employees.ToList();
        List<Employee> busEmployees = new List<Employee>();

        try
        {
            foreach (Employee emp in employees)
            {
                Employee empBus = new Employee();
                empBus.Title = emp.Title;
                empBus.FirstName = emp.FirstName;
                empBus.LastName = emp.LastName;
                empBus.PhoneNo = emp.PhoneNo;
                empBus.Email = emp.Email;
                empBus.DepartmentID = emp.DepartmentID;
                empBus.EmployeeID = emp.EmployeeID;
                busEmployees.Add(empBus);
            }//end foreach
        }//end try
        catch (Exception ex)
        {
            throw new Exception(ex.Message);
        }//end catch
        return busEmployees;
    }//end getallemployees

From HelpdeskBusinessuser contains an error (label)

    public List<EmployeeBusinessUser> GetAll()
    {

            EmployeeBusinessData empBD = new EmployeeBusinessData();
            List<Employee> empL = new List<Employee>();
            List<EmployeeBusinessUser> empBU = new List<EmployeeBusinessUser>();
            empL = empBD.GetAll(); /*error cant explicitly convert.  */
            try
            {
                foreach (Employee emps in empL)
                {
                    EmployeeBusinessUser empBus = new EmployeeBusinessUser();
                    empBus.Title = emps.Title;
                    empBus.FirstName = emps.FirstName;
                    empBus.LastName = emps.LastName;
                    empBus.PhoneNo = emps.PhoneNo;
                    empBus.Email = emps.Email;
                    empBus.DepartmentID = emps.DepartmentID;
                    empBU.Add(empBus);
                }//end foreach
            }//end try
            catch (Exception ex)
            {
                ErrorRoutine(ex, "EmployeeBusinessUser", "GetAll");
            }
            return empBU;
        }//end getallemployees

Anyone have an idea how to solve?

If UserEmployee inherits from Data.emp , you can do a simple cast like this:

List<Data.emp> listDataEmps = empBD.GetAll();
if (listDataEmps == null)
{
    ... bummer ...
}

IEnumerable<UserEmployee> enumerableUserEmps = (IEnumerable<UserEmployee>) listDataEmps;

But if UserEmployee does not inherit from Data.emp , you can use the System.Linq.Enumerable.Select extension method together with a converter method:

IEnumerable<UserEmployee> listUserEmps = listDataEmps.Select(ConvertDataEmpToUserEmp);

ConvertDataEmpToUserEmp is a method for converting the Data.emp data type into the UserEmployee data type:

static UserEmployee ConvertDataEmpToUserEmp(Data.emp item)
{
    ... do the conversion here ...
}

You will have to implement this Converter method according to your requirements.

Note, that the Select method does not create a second list in memory, but only creates an enumerator iterating over the original source list.

If your scenario would require an independent List instead of an Enumerator depending on the source list (for example, a scenario where the source list might be modified by another thread while the enumerator traverses it), you could additionally call the System.Linq.Enumerable.ToList extension method:

List<UserEmployee> listUserEmps = enumerableUserEmps.ToList();



Side note:

Casting a generic collection type into another is not possible. It doesn't matter if the collection type parameters inherit from each other or not. If it were, you could do funny things like that:

List<FileStream> listFS = new List<FileStream>();
List<object> listSomeObjects = (List<object>) listFS;

double d = 42.0;
listSomeObjects.Add((object) d); // <<-- This obviously doesn't make sense...

Mind you, that this silly example also implies that the List<FileStream> object implements a method Add(object item), which, well, doesn't make sense either...

EDIT: I updated my answer in response to MarcinJuraszek's feedback.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM