首页 文章

无法隐式将'Note6MVCApplication5.Models.SPGetEmpDetailsByEmpIdJoinResult'类型转换为'Note6MVCApplication5.Models.Emp'

提问于
浏览
-4
public ActionResult Edit(int? id)
        {
            Emp emp = db.SPGetEmpDetailsByEmpIdJoin(id).SingleOrDefault();
            ViewData["DeptId"] = new SelectList(db.SPGetAllDeptDetails().ToList(), "DeptId", "DeptName", emp.DeptId);
            return View(emp);
        }

错误:无法将类型'Note6MVCApplication5.Models.SPGetEmpDetailsByEmpIdJoinResult'隐式转换为'Note6MVCApplication5.Models.Emp'

为什么会出现这个错误?

我发布了MVGetEmpDetailsByEmpIdJoinResult()的定义,该定义出现在MVCDemoDB.Designer.cs中

public partial class SPGetEmpDetailsByEmpIdJoinResult
    {

        private int _EmpId;

        private string _EmpName;

        private string _EmpJob;

        private decimal _EmpSalary;

        private int _DeptId;

        private string _DeptName;

        public SPGetEmpDetailsByEmpIdJoinResult()
        {
        }

        [global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_EmpId", DbType="Int NOT NULL")]
        public int EmpId
        {
            get
            {
                return this._EmpId;
            }
            set
            {
                if ((this._EmpId != value))
                {
                    this._EmpId = value;
                }
            }
        }

        [global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_EmpName", DbType="VarChar(50) NOT NULL", CanBeNull=false)]
        public string EmpName
        {
            get
            {
                return this._EmpName;
            }
            set
            {
                if ((this._EmpName != value))
                {
                    this._EmpName = value;
                }
            }
        }

        [global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_EmpJob", DbType="VarChar(50) NOT NULL", CanBeNull=false)]
        public string EmpJob
        {
            get
            {
                return this._EmpJob;
            }
            set
            {
                if ((this._EmpJob != value))
                {
                    this._EmpJob = value;
                }
            }
        }

        [global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_EmpSalary", DbType="Money NOT NULL")]
        public decimal EmpSalary
        {
            get
            {
                return this._EmpSalary;
            }
            set
            {
                if ((this._EmpSalary != value))
                {
                    this._EmpSalary = value;
                }
            }
        }

        [global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_DeptId", DbType="Int NOT NULL")]
        public int DeptId
        {
            get
            {
                return this._DeptId;
            }
            set
            {
                if ((this._DeptId != value))
                {
                    this._DeptId = value;
                }
            }
        }

        [global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_DeptName", DbType="VarChar(50) NOT NULL", CanBeNull=false)]
        public string DeptName
        {
            get
            {
                return this._DeptName;
            }
            set
            {
                if ((this._DeptName != value))
                {
                    this._DeptName = value;
                }
            }
        }
    }

1 回答

  • 0

    它似乎是类型转换错误 . 我建议在下面的语句中声明'var'类型的变量而不是Emp

    Emp emp = db.SPGetEmpDetailsByEmpIdJoin(id).SingleOrDefault();
    

    这样的事情

    var emp = db.SPGetEmpDetailsByEmpIdJoin(id).SingleOrDefault();
    

    第二种方法

    定义扩展方法将SPGetEmpDetailsByEmpIdJoinResult强制转换为Emp,如下面的示例Emp类

    public class Emp
        {
            public int DeptId { get; set; }
    
            public int EmpId { get; set; }
    
            /*
             * define other properties
             */
        }
    
        public static Emp ToEmp(this SPGetEmpDetailsByEmpIdJoinResult empResult)
        {
            return new Emp() {EmpId = empResult.EmpId, DeptId = empResult.DeptId};
        }
    

    并调用扩展方法

    Emp emp = db.SPGetEmpDetailsByEmpIdJoin(id).SingleOrDefault().ToEmp();
    

    这将解决您的问题 . 希望这可以帮助 :)

相关问题