2016-11-07 44 views
0
[Route("api/Student/{name}")] 
public HttpResponseMessage Get(string name) 
{ 
    using (studentContext sc = new studentContext()) 
    { 
     var entity = sc.Students.Where(x => x.Name.StartsWith(name)) ; 

     if (entity != null) 
     { 
      return Request.CreateResponse(HttpStatusCode.OK, entity); 
     } 

     else 
     { 
      return Request.CreateResponse(HttpStatusCode.NotFound, "Staff with name:" + name.ToString() + "not found"); 
     } 
    } 
} 
+1

什麼excactly不工作?你收到錯誤嗎? – Marco

回答

1

使用ToList()可以從數據庫中獲取數據。

var entity = sc.Students.Where(x => x.Name.StartsWith(name)).ToList(); 
0

你可以這樣做:

[Route("api/Student/{name}")] 
public HttpResponseMessage Get(string name) 
{ 
    using (studentContext sc = new studentContext()) 
    { 
     var students = sc.Students.Where(x => x.Name.StartsWith(name)).ToList(); 

     if (students != null && students.Count() > 0) 
     { 
      return Request.CreateResponse(HttpStatusCode.OK, students); 
     }  
     else 
     { 
      return Request.CreateResponse(HttpStatusCode.NotFound, "Staff with name:" + name.ToString() + "not found"); 
     } 
    } 
} 
相關問題