Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/306.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在Json中获取计数返回匿名对象_C#_.net Core - Fatal编程技术网

C# 如何在Json中获取计数返回匿名对象

C# 如何在Json中获取计数返回匿名对象,c#,.net-core,C#,.net Core,在这里,我需要根据学校系统Id计算学校数量 public async Task<ICollection<SchoolCountVm>> GetSchoolCountBySchoolSystemId(Guid schoolSystemId) { var schools = _GpsContext.School.AsNoTracking().Where(x => x.SchoolSystemsID == schoolSystemId).Count(); retur

在这里,我需要根据学校系统Id计算学校数量

public async Task<ICollection<SchoolCountVm>> GetSchoolCountBySchoolSystemId(Guid schoolSystemId)
{
 var schools = _GpsContext.School.AsNoTracking().Where(x => x.SchoolSystemsID == schoolSystemId).Count();
  return ok(new { count = schools }); // Here problem is showing
  }
公共异步任务GetSchoolCountBySchoolSystemId(Guid schoolSystemId)
{
var schools=_GpsContext.School.AsNoTracking().Where(x=>x.SchoolSystemsID==schoolSystemId.Count();
返回ok(new{count=schools});//这里显示问题
}

您返回的是
IActionResult
类型,但您的签名是
Task

此不匹配显示返回
ok
函数时的错误

试试这个:

public IActionResult GetSchoolCountBySchoolSystemId(Guid schoolSystemId)
{
  var schools = _GpsContext.School.AsNoTracking().Where(x => x.SchoolSystemsID == schoolSystemId).Count();
  return ok(new { count = schools }); // Here problem is showing
}
如果需要的话,也有一种异步风格

您可以查看以下操作返回类型指南:

当一个操作中可能存在多个ActionResult返回类型时,IActionResult返回类型是合适的。ActionResult类型表示各种HTTP状态代码。从ActionResult派生的任何非抽象类都可以作为有效的返回类型。此类别中的一些常见返回类型是BadRequestResult(400)、NotFoundResult(404)和OkObjectResult(200)。或者,可以使用ControllerBase类中的便利方法从操作返回ActionResult类型。例如,返回BadRequest();是return new BadRequestResult()的简写形式


您返回的是
IActionResult
类型,但您的签名是
Task

此不匹配显示返回
ok
函数时的错误

试试这个:

public IActionResult GetSchoolCountBySchoolSystemId(Guid schoolSystemId)
{
  var schools = _GpsContext.School.AsNoTracking().Where(x => x.SchoolSystemsID == schoolSystemId).Count();
  return ok(new { count = schools }); // Here problem is showing
}
如果需要的话,也有一种异步风格

您可以查看以下操作返回类型指南:

当一个操作中可能存在多个ActionResult返回类型时,IActionResult返回类型是合适的。ActionResult类型表示各种HTTP状态代码。从ActionResult派生的任何非抽象类都可以作为有效的返回类型。此类别中的一些常见返回类型是BadRequestResult(400)、NotFoundResult(404)和OkObjectResult(200)。或者,可以使用ControllerBase类中的便利方法从操作返回ActionResult类型。例如,返回BadRequest();是return new BadRequestResult()的简写形式

您可以遵循或如果不想更改签名,则从
GetSchoolCountBySchoolSystemId
方法返回
Task
,而不是
Task

我假设在
SchoolCountVm
模型中,您的属性为
Count

public async Task<SchoolCountVm> GetSchoolCountBySchoolSystemId(Guid schoolSystemId)
{
    var schoolCount = await _GpsContext.School.AsNoTracking()
                        .Where(x => x.SchoolSystemsID == schoolSystemId)
                        .CountAsync();

    return new SchoolCountVm() { Count = schoolCount };
}
公共异步任务GetSchoolCountBySchoolSystemId(Guid schoolSystemId)
{
var schoolCount=wait_GpsContext.School.AsNoTracking()
.Where(x=>x.SchoolSystemsID==schoolSystemId)
.CountAsync();
返回新的SchoolCountVm(){Count=schoolCount};
}
您可以遵循,或者如果您不想更改签名,则从
GetSchoolCountBySchoolSystemId
方法返回
Task
,而不是
Task

我假设在
SchoolCountVm
模型中,您的属性为
Count

public async Task<SchoolCountVm> GetSchoolCountBySchoolSystemId(Guid schoolSystemId)
{
    var schoolCount = await _GpsContext.School.AsNoTracking()
                        .Where(x => x.SchoolSystemsID == schoolSystemId)
                        .CountAsync();

    return new SchoolCountVm() { Count = schoolCount };
}
公共异步任务GetSchoolCountBySchoolSystemId(Guid schoolSystemId)
{
var schoolCount=wait_GpsContext.School.AsNoTracking()
.Where(x=>x.SchoolSystemsID==schoolSystemId)
.CountAsync();
返回新的SchoolCountVm(){Count=schoolCount};
}

问题出在哪里?问题出在哪里?假设SchoolCountVm模型中有属性,因为Count是正确的,我通过存储库模式创建API,所以不能使用@Athanasios Kataras答案,这是MVC的解决方案。但在这里的代码中仍然显示以下错误:CS1061“Schools”不包含“Count”的定义,并且找不到接受“Schools”类型的第一个参数的可访问扩展方法“Count”(您是否缺少using指令或程序集引用?),您假定在SchoolCountVm模型中具有属性as Count是正确的,我是通过存储库模式创建API的,所以不能使用@Athanasios Kataras作为MVC的解决方案。但在您的代码中仍然显示以下错误:CS1061“Schools”不包含“Count”的定义,并且找不到接受“Schools”类型的第一个参数的可访问扩展方法“Count”(是否缺少using指令或程序集引用?)