简体   繁体   English

在linq查询中获取计数

[英]get count in linq query

i want to get count in linq query its work fine without comparing date in where condition but after comparing date it gives exception. 我想在linq查询中获得计数,但在不比较where条件下的日期的情况下它的工作正常,但是在比较日期后它给出了异常。

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();
     }
}

Error is : The specified type member ;Date; 错误是:指定的类型成员; Date; is not supported in LINQ to Entities. LINQ to Entities不支持。 Only initializers, entity members, and entity navigation properties are supported. 仅支持初始化程序,实体成员和实体导航属性。

You have to use EntityFunctions.TruncateTime to get the Date part of DateTime 您必须使用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();
}

Since DateTime.Now is Constant at the moment, you could also write this 由于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();   
}

It has nothing to do with the Count() function. 它与Count()函数无关。 As the exception says, its a DateTime problem. 如异常所示,它是一个DateTime问题。 Use DbFunctions.TruncateTime ( EntityFunctions.TruncateTime is deprecated since EF6) 使用DbFunctions.TruncateTime (从EntityFunctions.TruncateTime不推荐使用EntityFunctions.TruncateTime

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();
     }
}

Add following lines: 添加以下行:

var today = DateTime.Now.Date;

Then use today instead of DateTime.Now.Date into linq query. 然后使用今天而不是DateTime.Now.Date进入linq查询。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM