2015-09-28 99 views
-1

我試圖在網站界面上輸入員工姓時顯示部門名稱,這是我的位置有問題!!! 命名空間ExerciseDAL { 公共類DepartmentDAO {錯誤t CS0266 t無法將類型'System.Linq.IQueryable <string>'隱式轉換爲'ExerciseDAL.Department'

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 { 私人DepartmentDAO _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 { 公共類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 
+2

您能否提供實際的代碼而不是代碼的截圖?請參閱本文 - > http://stackoverflow.com/help/how-to-ask - 值得注意的是「幫助其他人重現問題」部分。 – Jesse

+0

好的,會做的,我只是認爲這可能會更容易的人:p – Gorilla

回答

1

你 「從...這裏......」 表達式返回幾個部門,但你只需要一個。

試試這個:

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

或者從部門更改reDpt對象類型的東西,允許多個值,例如列表。

相關問題