【问题标题】:Mysql to Linq queryMysql 到 Linq 查询
【发布时间】:2020-04-15 11:50:22
【问题描述】:

MySQL 查询

select MAX(os.aggregate_date) as lastMonthDay,os.totalYTD
  from (SELECT aggregate_date,Sum(YTD) AS totalYTD 
        FROM tbl_aggregated_tables
          WHERE subscription_type = 'Subcription Income'
        GROUP BY aggregate_date) as os
GROUP by MONTH(os.aggregate_date),YEAR(os.aggregate_date)
ORDER BY lastMonthDay;

转换为这个 LINQ 查询

var income = context.tbl_aggregated_tables
                   .Where(s => s.subscription_type == "Subcription Income")
                   .GroupBy(s => s.aggregate_date)
                   .Select(result => new
                   {
                       date = result.Key,
                       ytdsum = result.Select(x => x.YTD).Sum()
                   })
                   .GroupBy(s => new { month = s.date.Month, year = s.date.Year })
                   .Select(
                    // select max data and take its ytdsum value 
                        ).ToList();

第二个分组的目的是找出每个月的最大天数。 现在,如何在第二次分组后选择每个月的最大日期及其 ytdsum?

更新

income = context.tbl_aggregated_tables
                   .Where(s => s.subscription_type == "Subcription Income")
                   .GroupBy(s => s.aggregate_date)
                   .Select(result => new
                   {
                       date = result.Key,
                       ytdsum = result.Select(x => x.YTD).Sum()
                   })
                   .GroupBy(s => new { s.date.Month, s.date.Year })
                   .Select(
                     x => x.Max(s => s.date)
                   ).ToList()

这样它只返回日期,我无法返回列表的完整对象,包括 ytdSum。

【问题讨论】:

    标签: c# mysql linq


    【解决方案1】:

    这应该可行:

    var income = context.tbl_aggregated_tables
                       .Where(s => s.subscription_type == "Subcription Income")
                       .GroupBy(s => s.aggregate_date)
                       .Select(result => new
                       {
                           date = result.Key,
                           ytdsum = result.Select(x => x.YTD).Sum()
                       })
                       .GroupBy(s => new { month = s.date.Month, year = s.date.Year })
                       .Select(
                            x => x.OrderByDescending(k => k.date).First() 
                       ).ToList();
    

    income 现在是一个对象列表,每个对象都有dateytdsum

    【讨论】:

    • 它抛出一个异常,一旦最后一个选择被消除,它就可以正常工作
    • 有什么例外?
    • 首先我收到一条异常消息说“方法'First'只能用作最终查询操作。考虑在这种情况下使用方法'FirstOrDefault'”---替换了first()使用 FirstOrDefault() 再次引发异常,在 MySql.Data.MySqlClient.MySqlStream.ReadPacket() \r\n 在 MySql.Data.MySqlClient.NativeDriver.GetResult(Int32& affectedRow, Int64& insertedId)\r\n .....`
    【解决方案2】:

    这很好,但它比原始的 mysql 查询花费更多的时间。

     var income = context.tbl_aggregated_tables
                           .Where(s => s.subscription_type == "Subcription Income"
                            )
                           .GroupBy(s => s.aggregate_date)
                           .Select(result => new
                           {
                               date = result.Key,
                               ytdsum = result.Select(x => x.YTD).Sum()
                           })
                           .GroupBy(s => new { s.date.Month, s.date.Year })
                           .Select(
                             x => x.OrderByDescending(s => s.date)
                           )
                           .ToList()
                           .Select(el => el.FirstOrDefault())
                           .OrderBy(s=>s.date);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-06-30
      • 1970-01-01
      • 1970-01-01
      • 2016-01-31
      • 1970-01-01
      • 1970-01-01
      • 2010-09-15
      • 2010-11-12
      相关资源
      最近更新 更多