Can't get EntityFunctions.TruncateTime() to work

前端 未结 3 1567
Happy的楠姐
Happy的楠姐 2020-12-05 04:15

I am using Entity Framework Code First. Using LINQ to Entity I want to grab a record based on a DateTime value. Here is my current code:

/// 
         


        
相关标签:
3条回答
  • 2020-12-05 05:00
    DateTime? dt = DateTime.Parse(sText);
    campaigns = _CampaignMasterRepository.Get().OrderByDescending(a => a.LaunchOn).Where(a => a.UserId == obj_User.ID && a.CampaignStatus == (int)OmevoEnums.CampaignStatus.Sent && a.CampaignName.Contains(sText) || DbFunctions.TruncateTime(a.LaunchOn) == DbFunctions.TruncateTime(dt)).ToList();
    

    For getting the date results through Linq queries use this function.

    0 讨论(0)
  • 2020-12-05 05:06

    I faced this problem recently when I upgraded my Web Application from Entity Framework 5 to Entity Framework 6. Then, I realized that System.Data.Entity DLL needs to be removed from the application completely in order to work with Entity Framework 6. Entity Framework 6 is not part of .NET Framework anymore and therefore it is independent of System.Data.Entity dll. In order to Truncate time, you would need to use System.Data.Entity.DbFunctions.TruncateTime(...) method from EntityFramework.dll. Yes, that solved my problem.

    Bottom Line : If you are using Entity Framework 6, first REMOVE the reference System.Data.Entity DLL and then, in your code, replace EntityFunctions.TruncateTime(..) with System.Data.Entity.DbFunctions.TruncateTime(...). [ From EntityFramework.dll ]

    0 讨论(0)
  • 2020-12-05 05:12

    I didn't remove the reference System.Data.Entity, I just changed the call from DbFunctions.TruncateTime to System.Data.Entity.DbFunctions.TruncateTime and it worked for me.

    I'm using Entity 6.

    0 讨论(0)
提交回复
热议问题