【问题标题】:Count within a formatted DateTime with LINQ使用 LINQ 在格式化的 DateTime 内计数
【发布时间】:2018-03-24 07:38:11
【问题描述】:

我从数据库中填充以下列表,我希望仅按天(忽略时间)获取好或坏的百分比。

我无法按 data.GroupBy(p => p.DateTime.ToString("YYYY-MM-dd"))String.Format("YYYY-MM-dd", p.DateTime) 对数据进行分组或从中计数,更不用说执行 .Where 子句来匹配内容的好坏并从中计数。

在 LINQ 中甚至有可能吗?

我可以使用 SQL 来格式化每个 DateTime,执行多个 Array 和 Unique 转换以及多个 SQL 调用来检索每个变量的计数来计算比率,但这太愚蠢了。

DateTime                    Content
-------------------------------------
2018-03-16 17:59:26.000     Good
2018-03-16 18:05:04.000     Bad
2018-03-16 19:23:26.000     Bad
2018-03-17 03:19:02.000     Good
2018-03-17 06:20:32.000     Bad

我想得到什么

2018-03-16   Good: 33%   Bad: 66%
2018-03-17   Good: 50%   Bad: 50%

【问题讨论】:

  • 你遇到了什么错误?
  • data.GroupBy(p => p.DateTime.Date)
  • 您好,这些解决方案是否解决了您的问题?或者如果您设法以另一种方式解决此问题,请考虑添加您的解决方案并将其标记为未来读者的正确答案:)

标签: c# wpf linq


【解决方案1】:

基本上你想利用DateTime.Date

DateTime.Date Property

获取此实例的日期组件。

给定

var list = new List<MyClass>()
               {
                  new MyClass()
                     {
                        Content = "Good",
                        MyDateTimeField = DateTime.Now
                     },
                  new MyClass()
                     {
                        Content = "Good",
                        MyDateTimeField = DateTime.Now
                     },
                  new MyClass()
                     {
                        Content = "Bad",
                        MyDateTimeField = DateTime.Now
                     }
               };

分组方式

var results = list.GroupBy(x => x.MyDateTimeField.Date)
                  .Select(x =>
                              {
                                 var count = x.Count();
                                 var good = x.Count(y => y.Content == "Good");
                                 var bad = x.Count(y => y.Content == "Bad");
                                 var result = new
                                                {
                                                   Date = x.Key,
                                                   Good = good / (decimal)count,
                                                   Bad = bad / (decimal)count
                                                };
                                 return result;
                              });

foreach (var item in results)
{
     Console.WriteLine(string.Format("Date = {0}, Good = {1:P2}, Bad = {2:P2}", item.Date.Date, item.Good, item.Bad));
}

输出

Date = 3/24/2018 12:00:00 AM, Good = 66.67 %, Bad = 33.33 %

Full Demo Here

注意:我使用Format Specifiers in C# P2 来格式化百分比,但是您可以应用自己的四舍五入和逻辑来整理它

【讨论】:

    【解决方案2】:

    你可以试试

    var result = from p in dataContext.Table
                 group p by p.DateTime.Date into g
                 select new { 
                     Date = g.Key, 
                     NumberOfGood = g.Count(p => p.Content == "Good"), 
                     NumberOfBad = g.Count(p => p.Content == "Bad"), 
                     PercentageGood = ((decimal)g.Count(p => p.Content == "Good") / ((decimal)g.Count(p => p.Content == "Good") + (decimal)g.Count(p => p.Content == "Bad"))),
                     PercentageBad = ((decimal)g.Count(p => p.Content == "Bad") / ((decimal)g.Count(p => p.Content == "Good") + (decimal)g.Count(p => p.Content == "Bad"))) 
                 };
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-08-31
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多