简体   繁体   中英

Entity Framework DateTime Functions

I'm trying to filter by date based on two datetimes. MinTime and MaxTime are both nullable DateTimes in SQL, I want to filter based on these, but I'm getting an error:

This function can only be invoked from LINQ to Entities

public static IEnumerable<ExclusionRule> GetExclusionRules(AppointmentTypes? apptType, string resourceName, TimeSpan? minTime, TimeSpan? maxTime, DayOfWeek? day) {
    using (var db = new BusinessObjectContainer()) {
        db.ExclusionRules.MergeOption = MergeOption.NoTracking;
        var items = db.ExclusionRules;
        int intDay = day.HasValue ? (int) day.Value : -1,
            intApptType = apptType.HasValue ? (int)apptType.Value : -1;
        var filteredApptType = apptType.HasValue ? items.Where(i => !i.t_AppointmentType.HasValue|| i.t_AppointmentType == intApptType) : items;
        var filteredResource = string.IsNullOrWhiteSpace(resourceName) ? filteredApptType : filteredApptType.Where(i => !string.IsNullOrEmpty(i.ResourceName) && resourceName.ToLower().Equals(i.ResourceName.ToLower()));

        IEnumerable<ExclusionRule> filteredMinDate;
        if (minTime.HasValue) {
            filteredMinDate = filteredResource.Where(i => (!i.MinTime.HasValue) || (EntityFunctions.CreateTime(i.MinTime.Value.Hour, i.MinTime.Value.Minute, 0) >= minTime.Value));
        } else {
            filteredMinDate = filteredResource;
        }

        IEnumerable<ExclusionRule> filteredMaxDate;
        if (maxTime.HasValue) {
            // this throws the exception
            filteredMaxDate = filteredMinDate.Where(i => (!i.MaxTime.HasValue) || EntityFunctions.CreateTime(i.MaxTime.Value.Hour, i.MaxTime.Value.Minute, 0) <= maxTime.Value);
        } else {
            filteredMaxDate = filteredMinDate;
        }
        var filteredWeekDay= day.HasValue ? filteredMaxDate.Where(i => !i.t_DayOfWeek.HasValue|| i.t_DayOfWeek == intDay) : filteredMaxDate;
        return filteredWeekDay.ToList();

You are using EntityFunctions * in a linq statement on an IEnumerable<ExclusionRule> . But you can only use it on an IQueryable that has an Entity Framework query provider. So you should start with IQueryable<ExclusionRule> (from EF) or just create DateTime s in the regular.Net way.

* DbFunctions as of Entity Framework 6.

MinTime.Value.Hour etc. might have to be executed in a Query and it seems to me that you are excecuting them in .Net memory. That's why you get the error.

LINQ to entities: http://msdn.microsoft.com/en-us/library/bb386964.aspx

This function can only be invoked from LINQ to Entities

EntityFunctions is applicable is LINQ to Entities queries only.

The EntityFunctions class contains methods that expose canonical functions to use in LINQ to Entities queries. http://msdn.microsoft.com/en-us/library/dd456873.aspx

I got around this by calling .ToList() before the DateTime filtering. This pulls everything into .NET so the querying can be done there.

As there are a relatively small number of items in my database this wasn't a problem.

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