我有一个返回List的WCF服务。WCF OperationContract返回列表<CustomType>
[DataContract]
public class EmployeesVM
{
[DataMember]
public int ID { get; set; }
[DataMember]
public string Name { get; set; }
}
我正在使用EF将Employees列表返回给此类。在我的WCF服务我使用:
public IList<EmployessVM> getEmployees()
{
using (var dbContext = new SecEntities())
{
return (from e in dbContext.Employees
select new EmployeesVM {
ID = e.ID,
Name = e.Name
}).ToList();
}
}
在我的Windows Phone 8的客户端应用程序,我需要从方法列表。
void proxy_getEmployeesCompleted(object sender, GetDataService.getEmployeesCompletedEventArgs e)
{
if (e.Result != null)
{
List<ViewModel.EmployeesVM> resultList = e.Result.ToList();
}
}
在我的WP的ViewModel文件夹我有相同的类型:
public class EmployeesVM
{
public int ID { get; set; }
public string Name { get; set; }
}
但是,当我尝试编译我得到这个错误:
Cannot implicitly convert type 'System.Collections.Generic.List<SisSeguranca.GetDataService.EmployeesVM>' to 'System.Collections.Generic.List<SisSeguranca.ViewModel.EmployeesVM>'
我已经试过这也一样,并没有奏效。
List<ViewModel.EmployeesVM> lista = (ViewModel.EmployeesVM)e.Result.ToList();
我怎么能转换成具有相同的字段和名字我的客户端应用程序类型在WCF服务返回的类型?
可以在客户端和服务中使用相同的类型,也可以使用映射库。对我来说,我通常会返回一个DTO类,它出现在服务器和客户端上,并映射到模型中的实体类。 – Mashton 2014-08-29 14:00:30