Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.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# 如何使用entity framework core获取每个月的总回报数_C#_Linq_Entity Framework Core - Fatal编程技术网

C# 如何使用entity framework core获取每个月的总回报数

C# 如何使用entity framework core获取每个月的总回报数,c#,linq,entity-framework-core,C#,Linq,Entity Framework Core,我正试图获得给定数据集的每个月的实例数,但很难找到解决方案 这是我的实体框架模型 public class Data { public string Id { get; set; } public string Name { get; set; } public string Time { get; set; } public string Date { get; set; } } 这是我的方法 public ActionResult<IEnumerable&l

我正试图获得给定数据集的每个月的实例数,但很难找到解决方案

这是我的实体框架模型

public class Data 
{
   public string Id { get; set; }
   public string Name { get; set; }
   public string Time { get; set; }
   public string Date { get; set; }
}
这是我的方法

public ActionResult<IEnumerable<Result>> GetDates()
{
   IEnumerable<string> dates = _Database.result.Select(r => r.Date);
   IEnumerable<int> totalResult= _Database.result.Select(r => r.Date).Count();
   return Ok(date);
}
public ActionResult GetDates()
{
IEnumerable dates=\u Database.result.Select(r=>r.Date);
IEnumerable totalResult=_Database.result.Select(r=>r.Date.Count();
返回Ok(日期);
}
您有:

var totalResult= _Database.result.Select(r => r.Date).Count();
您应该将其更改为:

var totalResults = _Database.result
          .Where(r => DateTime.Parse(r.Date).Month == 4)
          .Count();
您也可以这样做:

var totalResults = _Database.result
           .Count(r => DateTime.Parse(r.Date).Month == 4);
这将
Date
属性解析为
DateTime
对象,并将过滤
DateTime.Month
等于4的结果,然后获取计数。您可以删除Linq语句中的
Select()
,因为如果只需要计数,则无需检索某些属性


Count()
将返回一个
int
值,而不是一个数组。

如果您对每个月的计数感兴趣,您应该可以只使用GroupBy

var dates = _Database.result.Select(r => r.Date).ToList();
IEnumerable<int> monthGrp = dates
   .GroupBy(e => DateTime.Parse(e).Month)
   .Select(e => e.Count());
var dates=\u Database.result.Select(r=>r.Date.ToList();
IEnumerable monthGrp=日期
.GroupBy(e=>DateTime.Parse(e).Month)
.Select(e=>e.Count());

我使用日期作为字符串-如果您想按分组(即获得每月的计数),则必须将字符串解析为日期
.count()//这将得到一个对象数组
-什么?Count是对不起,我的错,它返回的是int。这可以直接使用Count,就像方法可以使用一样-
.Count(r=>DateTime.Parse(r.Date).Month==4)
很高兴知道。谢谢,我将把它添加到我的答案中。这个答案不正确,因为它使用的是日期时间,而我使用的是日期作为字符串?我可能是错的或没有意义的lol。我无论如何都使用了该代码,但我得到了“.Where(r=>DateTime.Parse(r.Date.Month==4)”无法翻译。其他信息:方法“System.DateTime.Parse”的翻译失败。抱歉,我只是一个新手,我会尽我最大的努力去理解和使用它。这很有趣,当你编译/构建的时候有没有错误?或者它是在运行时发生的?之所以这样做,是因为它是一个DateTime.Parse,所以它会把你的字符串值解析成一个DateTime对象。当我在swagger.net内核中点击execute时就会发生这种情况。如果我没有构建错误。在我点击端点检索数据之后。哦,我明白了,谢谢你在结尾提供了额外的信息。:(相同的错误:.GroupBy(keySelector:r=>DateTime.Parse(r.Date).Month,elementSelector:r=>r.Date)'无法翻译。其他信息:翻译方法'System.DateTime.Parse'失败。如果此方法可以映射到自定义函数,翻译方法'System.DateTime.Parse'失败。如果此方法可以映射到自定义函数,请以可以翻译的形式重写查询,或切换到客户端ev通过插入对“AsEnumerable”、“AsAsAsAsAsyncEnumerable”、“ToList”或“ToListSync”的调用来显式计算。嗯,错误听起来很熟悉,我猜你在使用实体框架?对我的答案进行了调整,应该可以解决这个错误完美的人,它可以工作是的我在使用实体框架,谢谢你的帮助耶记住实体框架与执行不同,我们只是添加到查询中,没有枚举。SQL不理解DateTime.Parse()