简体   繁体   中英

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.

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; is not supported in 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

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

 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. As the exception says, its a DateTime problem. Use DbFunctions.TruncateTime ( EntityFunctions.TruncateTime is deprecated since 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();
     }
}

Add following lines:

var today = DateTime.Now.Date;

Then use today instead of DateTime.Now.Date into linq query.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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