5
 var res = from r in db.myTable
                  group r by new
                  {
                      Year = r.DateVal.Year,
                      Month = r.DateVal.Month,
                      Day = r.DateVal.Day
                  } into g
                  let Count = g.Count()
                  select new
                  {
                      Year = g.Key.Year,
                      Month = g.Key.Month,
                      Day = g.Key.Day,
                      Count = Count
                  };

不工作。

内部异常的摘录:

    InnerException: MySql.Data.MySqlClient.MySqlException
    HResult=-2147467259
    Message=Unknown column 'GroupBy1.K1' in 'field list'

该查询生成以下 SQL:

SELECT
    1 AS `C1`, 
    `GroupBy1`.`K1` AS `C2`, 
    `GroupBy1`.`K2` AS `C3`, 
    `GroupBy1`.`K3` AS `C4`, 
    `GroupBy1`.`A1` AS `C5`
    FROM (SELECT
        COUNT(1) AS `A1`
        FROM `myTable` AS `Extent1`
        GROUP BY 
        YEAR(`Extent1`.`DateVal`), 
        MONTH(`Extent1`.`DateVal`), 
        DAY(`Extent1`.`DateVal`)) AS `GroupBy1`

**

Zach 建议的 Linq 查询:

**

    var test = from r in db.myTable
                   group r by new
                   {
                       Year = r.DateCol.Year,
                       Month = r.DateCol.Month,
                       Day = r.DateCol.Day
                   } into grp
                   select new
                   {
                       Year = grp.Key.Year,
                       Month = grp.Key.Month,
                       Day = grp.Key.Day,
                       Count = grp.Count()
                   };
        try
        {
            var test2 = test.ToList();
        }
        catch (Exception err)
        {

        }

生成的 SQL:

    SELECT
    1 AS `C1`, 
    `GroupBy1`.`K1` AS `C2`, 
    `GroupBy1`.`K2` AS `C3`, 
    `GroupBy1`.`K3` AS `C4`, 
    `GroupBy1`.`A1` AS `C5`
    FROM (SELECT
    COUNT(1) AS `A1`
    FROM `myTable` AS `Extent1`
     GROUP BY 
    YEAR(`Extent1`.`DateCol`), 
    MONTH(`Extent1`.`DateCol`), 
    DAY(`Extent1`.`DateCol`)) AS `GroupBy1`

捕获的异常:

System.Data.EntityCommandExecutionException was caught
  HResult=-2146232004
  Message=An error occurred while executing the command definition. See the inner exception for details.
  Source=System.Data.Entity
  StackTrace:
       at System.Data.EntityClient.EntityCommandDefinition.ExecuteStoreCommands(EntityCommand entityCommand, CommandBehavior behavior)
       at System.Data.Objects.Internal.ObjectQueryExecutionPlan.Execute[TResultType](ObjectContext context, ObjectParameterCollection parameterValues)
       at System.Data.Objects.ObjectQuery`1.GetResults(Nullable`1 forMergeOption)
       at System.Data.Objects.ObjectQuery`1.System.Collections.Generic.IEnumerable<T>.GetEnumerator()
       at System.Data.Entity.Internal.Linq.InternalQuery`1.GetEnumerator()
       at System.Data.Entity.Infrastructure.DbQuery`1.System.Collections.Generic.IEnumerable<TResult>.GetEnumerator()
       at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
       at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
       at myNamespace._Default.fillChart(String username, Int32 tzClient) in e:\...[Path on my harddrive]..\Default.aspx.cs:line 102
  InnerException: MySql.Data.MySqlClient.MySqlException
       HResult=-2147467259
       Message=Unknown column 'GroupBy1.K1' in 'field list'
       Source=MySql.Data
       ErrorCode=-2147467259
       Number=1054
       StackTrace:
            at MySql.Data.MySqlClient.MySqlStream.ReadPacket()
            at MySql.Data.MySqlClient.NativeDriver.GetResult(Int32& affectedRow, Int64& insertedId)
            at MySql.Data.MySqlClient.Driver.GetResult(Int32 statementId, Int32& affectedRows, Int64& insertedId)
            at MySql.Data.MySqlClient.Driver.NextResult(Int32 statementId, Boolean force)
            at MySql.Data.MySqlClient.MySqlDataReader.NextResult()
            at MySql.Data.MySqlClient.MySqlCommand.ExecuteReader(CommandBehavior behavior)
            at MySql.Data.Entity.EFMySqlCommand.ExecuteDbDataReader(CommandBehavior behavior)
            at System.Data.Common.DbCommand.ExecuteReader(CommandBehavior behavior)
            at System.Data.EntityClient.EntityCommandDefinition.ExecuteStoreCommands(EntityCommand entityCommand, CommandBehavior behavior)
       InnerException: 
4

2 回答 2

2

在 Count() 中添加条件:

g.Count(_ => true)

最终解决方案将是:

var test = from r in db.myTable
               group r by new
               {
                   Year = r.DateCol.Year,
                   Month = r.DateCol.Month,
                   Day = r.DateCol.Day
               } into grp
               select new
               {
                   Year = grp.Key.Year,
                   Month = grp.Key.Month,
                   Day = grp.Key.Day,
                   Count = grp.Count(_ => true)
               };
    try
    {
        var test2 = test.ToList();
    }
    catch (Exception err)
    {

    }

不知道如何,但这为我解决了问题!

于 2017-01-03T15:53:21.657 回答
0

嗯,我可能会在您的其他代码的上下文之外考虑这个问题,但我认为不需要“let Count = g.Count()”语句。我认为这可能是 SQL 翻译搞砸的地方。将 g.Count() 函数向下移动到您在 select 子句中将其分配给 Count var 的分配。此外,您在 linq 中分配两个计数变量的事实看起来很麻烦......也许您尝试过这个,但我希望它有所帮助。

像这样...

var res = from r in db.myTable
              group r by new
              {
                  y = r.DateVal.Year,
                  m = r.DateVal.Month,
                  d = r.DateVal.Day
              } into g
              select new
              {
                  Year = g.Key.y,
                  Month = g.Key.m,
                  Day = g.Key.d,
                  Count = g.Count()
              };
于 2013-04-02T20:11:17.120 回答