【问题标题】:get count in linq query在 linq 查询中获取计数
【发布时间】:2016-11-10 20:36:21
【问题描述】:

我想在 linq 查询中获得计数,它的工作正常而不比较条件中的日期,但在比较日期之后它给出了异常。

public static IList GetAllCategoryData()
{
     using (var objEntity = new BlueCouponEntities())
     {
       return (from TBL in objEntity.CategoryMasters.AsEnumerable()
                 let IIT = TBL.CategoryImageTransactions
                 select new
                 {
                 TBL.CategoryID,
                 TBL.CategoryName,
                 CategoryCount = objEntity.OfferCategoryMasters.Where(Lg => Lg.CategoryID == TBL.CategoryID && Lg.OfferMaster.EndDate > DateTime.Now.Date).Count(),
                 }
             ).ToList();
     }
}

错误是:指定的类型成员;日期;在 LINQ to Entities 中不受支持。仅支持初始化程序、实体成员和实体导航属性。

【问题讨论】:

  • SQL-to-Entities 不支持 Date 类型。
  • @WicherVisser 在.NET 标准库中没有Date 类型,它是DateTime 结构上的实例属性,返回与当前实例相同日期的DateTime,但是时间在哪里00:00:00

标签: c# linq lambda


【解决方案1】:

您必须使用 EntityFunctions.TruncateTime 来获取 DateTime 的 Date 部分

using (var objEntity = new BlueCouponEntities())
{
    return (from TBL in objEntity.CategoryMasters.AsEnumerable()
                 let IIT = TBL.CategoryImageTransactions
                 select new
                 {
                 TBL.CategoryID,
                 TBL.CategoryName,
                 CategoryCount = objEntity.OfferCategoryMasters.Where(Lg => Lg.CategoryID == TBL.CategoryID && Lg.OfferMaster.EndDate > EntityFunctions.TruncateTime(DateTime.Now)).Count(),
                 }
             ).ToList();
}

由于 DateTime.Now 现在是常量,你也可以这样写

 using (var objEntity = new BlueCouponEntities())
 {
     DateTime now = DateTime.Now.Date;
     return (from TBL in objEntity.CategoryMasters.AsEnumerable()
                 let IIT = TBL.CategoryImageTransactions
                 select new
                 {
                 TBL.CategoryID,
                 TBL.CategoryName,
                 CategoryCount = objEntity.OfferCategoryMasters.Where(Lg => Lg.CategoryID == TBL.CategoryID && Lg.OfferMaster.EndDate > now).Count(),
                 }
             ).ToList();   
}

【讨论】:

  • 第二个示例将解决一个罕见的问题,即查询跨越午夜运行。
  • 这是一种过于复杂的方法。问题是 Date 在 L2E 中不是一个公认的属性,但是没有什么可以阻止您将 DateTime.Now.Date 分配给一个局部变量并在查询中使用它,就像在@BappyDatta 的回答中一样。
【解决方案2】:

它与 Count() 函数无关。正如例外所说,它的 日期时间问题。使用DbFunctions.TruncateTimeEntityFunctions.TruncateTime 自 EF6 起已弃用)

public static IList GetAllCategoryData()
{
     using (var objEntity = new BlueCouponEntities())
     {
       return (from TBL in objEntity.CategoryMasters.AsEnumerable()
                 let IIT = TBL.CategoryImageTransactions
                 select new
                 {
                 TBL.CategoryID,
                 TBL.CategoryName,
                 CategoryCount = objEntity.OfferCategoryMasters.Where(Lg => Lg.CategoryID == TBL.CategoryID && EntityFunctions.TruncateTime(Lg.OfferMaster.EndDate) > EntityFunctions.TruncateTime(DateTime.Now.Date)).Count(),
                 }
             ).ToList();
     }
}

【讨论】:

    【解决方案3】:

    添加以下行:

    var today = DateTime.Now.Date;
    

    然后使用 today 而不是 DateTime.Now.Date 进入 linq 查询。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-11-18
      • 1970-01-01
      • 1970-01-01
      • 2017-01-20
      • 1970-01-01
      • 2014-08-02
      • 1970-01-01
      相关资源
      最近更新 更多