.net 如何在使用FindAsync时获取IEnumerable
如何获得IEnumerable 寻找类似于返回IEnumerable的.net 如何在使用FindAsync时获取IEnumerable,.net,api,core,.net,Api,Core,如何获得IEnumerable 寻找类似于返回IEnumerable的 public async Task<ActionResult<IEnumerable<EclSmsConfig>>> GetEclSmsConfig() { return await _context.EclSmsConfig.ToListAsync(); } public异步任务GetEclSmsConfig() { 返回wait
public async Task<ActionResult<IEnumerable<EclSmsConfig>>> GetEclSmsConfig()
{
return await _context.EclSmsConfig.ToListAsync();
}
public异步任务GetEclSmsConfig()
{
返回wait_context.EclSmsConfig.ToListAsync();
}
如何在.NETCore3.0中使用以下代码获取IEnumerable
public async Task<ActionResult<emp>> GetEclSmsConfig(int id)
{
var Emp = await _context.emp.FindAsync(id);
if (Emp == null)
{
return NotFound();
}
return Emp;
}
public异步任务GetEclSmsConfig(int-id)
{
var Emp=wait_context.Emp.FindAsync(id);
如果(Emp==null)
{
返回NotFound();
}
返回Emp;
}
以下是有效的解决方案
public ActionResult<IEnumerable<emp>> Getemp(int id)
{
IQueryable<emp> filterdata;
if (id >= 1)
{
filterdata = from s in _context.emp
where (s.JobId == id)
select s;
}
else
{
filterdata = from s in _context.emp
select s;
};
var output = filterdata.ToArray();
if (output.Length < 1)
{
return NotFound();
}
return output;
}
public ActionResult Getemp(int-id)
{
液体过滤数据;
如果(id>=1)
{
filterdata=来自_context.emp中的s
其中(s.JobId==id)
选择s;
}
其他的
{
filterdata=来自_context.emp中的s
选择s;
};
var输出=filterdata.ToArray();
如果(输出长度<1)
{
返回NotFound();
}
返回输出;
}
我认为情况如下:
public async Task<ActionResult<IEnumerable<EclSmsConfig>>> GetEclSmsConfig(int id)
{
var result = await _context.emp.Where(e => e.Id == id).ToListAsync();
if(!result.Any())
{
return NotFound()
}
return result;
}
public异步任务GetEclSmsConfig(int-id)
{
var result=await_context.emp.Where(e=>e.Id==Id.toListSync();
如果(!result.Any())
{
returnnotfound()
}
返回结果;
}
如果您要参与开发前端,您应该进行重构,以更新前端代码以使用单个记录进行处理,而不是要求后端api返回集合,以防您确定is始终只有一个返回的记录 是的,如果没有IEnumerable,我不会在json中得到outer[],并且在角度端得到错误(不知道如何解决这个问题)