WebApi 2使用实体框架检索

时间:2016-01-23 11:06:41

标签: c# sql-server linq asp.net-web-api

我想要实现的是从数据库中检索所有数据,截至目前我知道我需要替换

  

的SingleOrDefault

用别的东西,我可以知道下面叫做的方法是什么?我不认为是linq?

[Route("api/{AuthCode}/LoadWorkers")]
public Task<HttpResponseMessage> GET(String Authcode)
{
    DateTime futureDate = new DateTime();
    futureDate = DateTime.Now.AddDays(90);
    worker result = new worker();
    result = KKDB.workers.SingleOrDefault(p => p.WWPED <= futureDate);
    return Task.FromResult(Request.CreateResponse<worker>(HttpStatusCode.OK, result));
}

解决方案

[Route("api/{AuthCode}/LoadWorkers")]
public Task<HttpResponseMessage> GET(String Authcode)
{
  DateTime futureDate = new DateTime();
  futureDate = DateTime.Now.AddDays(90);
  var result = KKDB.workers.Where(x => x.WWPED <= futureDate);
  return Task.FromResult(Request.CreateResponse<IQueryable<worker>>HttpStatusCode.OK,result));
}

1 个答案:

答案 0 :(得分:0)

尝试使用可以获取实体的.Where(x => x.WWPED <= futureDate),然后您可以将它们映射到所需的格式以便返回结果。使用linq可能会更容易。