错误CS0266无法将类型'System.Linq.IQueryable <string>'隐式转换为'ExerciseDAL.Department'

时间:2015-09-28 02:19:59

标签: javascript java jquery client-server

当我在网站界面输入员工姓氏时,我试图显示部门名称,这就是我遇到问题的地方! 名称空间ExerciseDAL {     公共类DepartmentDAO     {

    public DepartmentDAO() { }

    public Department GetById(string departId)
    {

        Department reDpt = null;
        DbContext _ctx;

        try
        {
            _ctx = new DbContext();
            var departments = _ctx.Departments;
            var dept = departments.AsQueryable<Department>().FirstOrDefault(dpt => dpt.Id.ToString() == departId);
            reDpt = dept;
        }
        catch (Exception ex)
        {
            Console.WriteLine("Problem " + ex.Message);
        }
        return reDpt;
    }
}
}

正如你所看到的那样,我正在获得错误CS0266。 dept.id是一个对象,我必须将它与一个字符串进行比较,但是当我这样做时它不会编译。

系类:

 namespace ExerciseDAL
{
public class Department
 {
    public ObjectId Id { get; set; } //represents primary key
    public string DepartmentName { get; set; }
}
}

DepartmentViewModel:我也将在这里更改代码,因为我知道这是错误的。

命名空间ExerciseViewModel {     公共类DepartmentViewModel     {         私人部门_DAO;

    public string DepartmentsId { get; set; } //represents primary key
    public string DepartmentName { get; set; }

    public DepartmentViewModel()
    {
        _daos = new DepartmentDAO();
    }



    public void GetByDepart()
    {
        try
        {
            Department dpt = _daos.GetById(DepartmentsId);
            DepartmentsId = dpt.Id.ToString();
            DepartmentName = DepartmentName;
        }
        catch (Exception ex)
        {
            DepartmentsId = "not Found!";
        }
    }
}
}

DepartmentController:我也将在这里更改代码,因为我知道这是错误的。

名称空间ExerciseWebSite {     公共类DepartmentController:ApiController     {

    [Route("api/department/{departmentId}")]
    public IHttpActionResult Get(string departmentId)
    {
        try
        {
            DepartmentViewModel dpt = new DepartmentViewModel();
            dpt.DepartmentsId = departmentId;
            dpt.GetByDepart();
            return Ok(dpt);
        }
        catch (Exception ex)
        {
            return BadRequest("retrieve faild - " + ex.Message);
        }
    }

}
}

Employee.js:

 $(function () {
$("#empbutton").click(function (e) {
    var last = $("#TextBoxLastname").val();
    ajaxCall("Get", "api/employees/" + last, "").done(function (data) {
        if (data.Lastname !== "not found") {
            $("#email").text(data.Email);
            $("#title").text(data.Title);
            $("#firstname").text(data.Firstname);
            $("#phone").text(data.Phoneno);

            ajaxCall("Get", "api/department/" + data.Id)
            .done(function (depdata)
            {
                $("#departmentname").text(depdata.DepartmentName);
            })
            .fail(function (jqXHR, textStatus, errorThrown)
            { errorRoutine(jqXHR); });
        }//end of if
        else {
            $("#firstname").text("Not Found");
            $("#email").text("");
            $("#title").text("");
            $("#phone").text("");
            $("#departmentname").text("");
        }//end of else
    }).fail(function (jqXHR, textStatus, errorThrown)
    { errorRoutine(jqXHR); });//end of ajax

1 个答案:

答案 0 :(得分:1)

您的“from ... where ...”表达式会返回多个部门,但您只需要一个部门。

试试这个:

var dept = departments.Where(x=>x.Id == departId).Single();

或者将您的reDpt对象类型从Department更改为允许多个值的内容,例如List。