2013-10-18 122 views
0

現在一直存在此錯誤。 基本上我有2 C#類庫 HeldDeskBusinessData和HelpDeskBusinessUser(縮短標題)無法將類型'System.Collections.Generic.List <Data.emp>'隱式轉換爲'System.Collections.Generic.List <User.Employee>'

從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 

從HelpdeskBusinessuser包含一個錯誤(標籤)

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 

人有一個想法怎麼解決?

+1

它在'HelpDeskBusinessData'中的哪一行會引發錯誤?這裏沒有任何「Data.Emp」類。這是你正在使用的課程嗎? –

+0

@JeroenVannevel:他們縮短了標題。 – ChiefTwoPencils

+0

@BobbyDigital:我不會認爲,考慮命名空間也是不同的。 –

回答

0

如果UserEmployee從Data.emp繼承,你可以做一個簡單的鑄像這樣:

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

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

但如果UserEmployee不從繼承Data.emp,您可以使用System.Linq.Enumerable.Select擴展方法連同轉換器方法:

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

ConvertDataEmpToUserEmp對於Data.emp數據類型轉換爲UserEmployee數據類型的方法:

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

你必須根據你的要求來實現這種轉換方法。

請注意,Select方法不會在內存中創建第二個列表,但只會創建迭代原始源列表的枚舉器。

如果您的方案需要一個獨立的List而不是Enumerator,具體取決於源列表(例如,源列表可能會在枚舉器遍歷時由另一個線程修改),那麼您可以另外調用System .Linq.Enumerable.ToList擴展方法:

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



旁註:

鑄造泛型集合類型到另一個是不可能的。 集合類型參數是否相互繼承無關緊要。 如果是這樣,你可以做有趣的事情那樣:

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... 

你要知道,這個愚蠢的例子也意味着列表<的FileStream >對象實現的方法添加(對象項),其中,好了,沒有按沒有任何意義...

編輯:我更新了我的回答,以迴應MarcinJuraszek的反饋。

+1

對*旁註*的評論:您可以使用IEnumerable '這樣做,因爲它設置爲協變。這是可能的,因爲'IEnumerable '不公開任何修改集合的方法。你可以用引用類型的數組來做到這一點,因爲.NET中的數組協變 - 但它會引入運行時類型檢查,並使性能變得更糟。 – MarcinJuraszek

相關問題