WebApi 2 使用实体框架检索
本文关键字:框架 检索 实体 WebApi | 更新日期: 2023-09-27 18:31:37
我想要实现的是,要从数据库中检索所有数据,到目前为止,我知道我需要替换
单或默认
用别的东西,我可以知道下面的方法叫什么吗?我不认为是林克?
[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));
}
尝试使用.Where(x => x.WWPED <= futureDate)
来获取实体,然后可以将它们映射到所需的格式以返回结果。 使用 linq 可能更容易。