处理转换

时间:2018-09-04 10:31:39

标签: c# asp.net-web-api

在vb中:

Dim mEmpLeaves As EmployeeLeaves
mEmpLeaves = EmployeeLeaves.GetEmployeeLeaves(Session("DbConnector"), 
              "dbo.usp_EmployeeWebLeave" & EmployeeId)
ViewState("Empleave") = mEmpLeaves

在diff函数中,我使用这种视图状态:

Dim mEmployeeLeaves As EmployeeLeaves = DirectCast(ViewState("Empleave"), 
                                    EmployeeLeaves)
Dim mEmployeeLeave As EmployeeLeave = 
                             mEmployeeLeaves.GetEmployeeLeaveById(leaveId)

由于我由于其无状态行为而未在其API中使用viewstate,而且我听说这也不正确。我该如何实现?

我在做什么

EmployeeLeaves mEmployeeLeaves = DbEntities.Usp_EmployeeWebLeave(EmployeeId)
                                 .FirstOrDefault();
mEmployeeLeave mEmployeeLeave= mEmployeeLeaves.GetEmployeeLeaveById(LeaveId)

也尝试过:

object Leaves = DbEntities.Usp_EmployeeWebLeave(EmployeeId)
                                 .FirstOrDefault();
EmployeeLeaves mEmployeeLeaves = (EmployeeLeaves)Leaves;
EmployeeLeave mEmployeeLeave= mEmployeeLeaves.GetEmployeeLeaveById(LeaveId);

我要例外了 “

  

无法转换对象类型   键入“ employeeDataAccess.payCare.usp_EmployeeWebLeave_Result”   'HumanResource.Paycare.EmployeeLeaves

我没有找到一种方法来投射它。在这种情况下,vb似乎是如此简单。浪费了我整天的时间。如果有人可以将我从中删除,请。预先感谢。

1 个答案:

答案 0 :(得分:0)

由于它们都是不同的类型,因此应手动映射它们,

var Leaves = DbEntities.Usp_EmployeeWebLeave(EmployeeId)
                             .FirstOrDefault();
EmployeeLeaves mEmployeeLeaves=Converter(Leaves)






EmployeeLeaves Converter(usp_EmployeeWebLeave_Result e)
{
   return new
             HumanResource.Paycare.EmployeeLeaves(){
                   Name=leaves.Name,
                   Age=leaves.Age
                   ...........
          }   ;
}