简体   繁体   English

WebApi 2使用实体框架检索

[英]WebApi 2 using entity Framework Retrieving

What I want to achieve is that to retrieve all data from the database, as of now I know that I need to replace 我想要实现的是从数据库中检索所有数据,到目前为止,我知道我需要替换

SingleOrDefault 单一或默认

with something else and can I know what is the method below called ? 与其他东西,我可以知道下面叫什么方法吗? I don't think is linq ? 我不认为是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));
}

Solution

[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));
}

Try using .Where(x => x.WWPED <= futureDate) that will get you the entities, you can then map them into the desired format for returning as result. 尝试使用.Where(x => x.WWPED <= futureDate)可以获取实体,然后可以将它们映射为所需格式以作为结果返回。 This might be easier using linq. 使用linq可能会更容易。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM