错误 CS0266 无法将类型 'System.Linq.IQueryable<string>' 隐式转换为 'ExerciseDAL.Department'
Error CS0266 Cannot implicitly convert type 'System.Linq.IQueryable<string>' to 'ExerciseDAL.Department'
当我在网站界面上输入员工姓氏时,我试图显示部门名称,这就是我遇到问题的地方!
命名空间 ExerciseDAL
{
public class 部门DAO
{
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 是一个对象,我必须将它与字符串进行比较,但是当我这样做时它无法编译。
部门Class:
namespace ExerciseDAL
{
public class Department
{
public ObjectId Id { get; set; } //represents primary key
public string DepartmentName { get; set; }
}
}
DepartmentViewModel:我也将更改此处的代码,因为我知道这是错误的。
命名空间 ExerciseViewModel
{
public class DepartmentViewModel
{
私人部门DAO _daos;
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
{
public class 部门控制器: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
你的"from...where..."表达returns几个部门,但你只需要一个。
试试这个:
var dept = departments.Where(x=>x.Id == departId).Single();
或者将您的 reDpt 对象类型从 Department 更改为允许多个值的类型,例如 List。
当我在网站界面上输入员工姓氏时,我试图显示部门名称,这就是我遇到问题的地方! 命名空间 ExerciseDAL { public class 部门DAO {
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 是一个对象,我必须将它与字符串进行比较,但是当我这样做时它无法编译。
部门Class:
namespace ExerciseDAL
{
public class Department
{
public ObjectId Id { get; set; } //represents primary key
public string DepartmentName { get; set; }
}
}
DepartmentViewModel:我也将更改此处的代码,因为我知道这是错误的。
命名空间 ExerciseViewModel { public class DepartmentViewModel { 私人部门DAO _daos;
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 { public class 部门控制器: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
你的"from...where..."表达returns几个部门,但你只需要一个。
试试这个:
var dept = departments.Where(x=>x.Id == departId).Single();
或者将您的 reDpt 对象类型从 Department 更改为允许多个值的类型,例如 List。