我是要查询表里边 时间字段TimeBook,所有的月份列表,包括年份,下边只有月份,还是int类型; 能不能包含年月的时间类型我查询了下定义:
        // 摘要:
        //     获取此实例所表示日期的月份部分。
        //
        // 返回结果:
        //     月组成部分,表示为 1 和 12 之间的一个值。
        public int Month { get; }
magaView.HasMonth = db.Library.GroupBy(a => a.TimeBook.Month).Select(a => a.Key).ToList(); 

解决方案 »

  1.   

    db.Library.GroupBy(a => new DateTime(a.TimeBook.Year, a.TimeBook.Month, 1)).Select(a => a.Key).ToList(); 
      

  2.   

    GroupBy(a => a.TimeBook.ToString("yyyy-MM"))
      

  3.   

    var qurey = from row in db.Library.AsEnumerable()
                            group row by row.timeBook
                                into g
                                select new
                                {
                                    dd = string.Format(@"{0}-{1}", g.Key.Year, g.Key.Month)
                                };
      

  4.   

    var qurey = from row in table.AsEnumerable()
                            group row by row.timeBook
                                into g
                                select new
                                {
                                    dd = string.Format(@"{0}-{1}", g.Key.Year, g.Key.Month)
                                }.dd.ToList();
      

  5.   


    出现异常:magaView.HasMonth = db.Library.Where(a => a.CategoryID == categoryId).GroupBy(a => a.TimeBook.ToString("yyyy-MM")).Select(a => a.Key).ToList();
    LINQ to Entities 不识别方法“System.String ToString(System.String)”,因此该方法无法转换为存储表达式。 
    说明: 执行当前 Web 请求期间,出现未经处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。 异常详细信息: System.NotSupportedException: LINQ to Entities 不识别方法“System.String ToString(System.String)”,因此该方法无法转换为存储表达式。
      

  6.   

    换一个方法还是不行
    magaView.HasMonth = db.Library.Where(a => a.CategoryID == categoryId).GroupBy
    (a => new DateTime(a.TimeBook.Year, a.TimeBook.Month, 1)).Select(a => a.Key).ToList();LINQ to Entities 仅支持无参数构造函数和初始值设定项。 
    说明: 执行当前 Web 请求期间,出现未经处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。 异常详细信息: System.NotSupportedException: LINQ to Entities 仅支持无参数构造函数和初始值设定项。源错误: 
    行 78:                 magaView.Magazine = db.Library.Where(a => a.TimeBook.Month==dt.Month && a.TimeBook.Year==dt.Year && a.CategoryID==categoryId).ToList();
    行 79: 
    行 80:                 magaView.HasMonth = db.Library.Where(a => a.CategoryID == categoryId).GroupBy(a => new DateTime(a.TimeBook.Year, a.TimeBook.Month, 1)).Select(a => a.Key).ToList();
    行 81:                
    行 82:                 return magaView;
     
      

  7.   

    原来你用EF啊
    magaView.HasMonth = db.Library.Where(a => a.CategoryID == categoryId).GroupBy(a => a.DateTime.Year * 24 + a.DateTime.Mouth).Select(a => a.Key).ToList().Select(a => new DateTime((int)Math.Floor(t / 24.0), t % 24, 1)).ToList();
      

  8.   


    是的,该怎么弄啊??  谢谢主要这里用的是group by,也不能在外边转换好再放到 lambda里边
      

  9.   

    优化一下
    db.Library.Where(a => a.CategoryID == categoryId).GroupBy(a => a.DateTime.Year * 12 + a.DateTime.Mouth - 1).Select(a => a.Key).ToList().Select(a => new DateTime((int)Math.Floor(t / 12.0), t % 12 + 1, 1)).ToList();