0

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

4 Answers 4

2

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 DateTimes in the regular.Net way.

*DbFunctions as of Entity Framework 6.

Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

0

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

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.