0

I'm trying to run a query on the database server side to load the entire filtered result into the application. I tried to perform filtering without using my own methods and it worked without errors. But if I use my own method in Expression Tree, I get this error during execution:

Failure: Direct client side execution of 'eventItem.GetEventOfficesIds()' failed

private static IQueryable<Guid> GetEventOfficesIds(this Event eventItem)
{
    IQueryable<Guid> officesIds;
    if (eventItem.GetValue<bool?>("IsPublicNews") != null &&
        eventItem.GetValue<bool?>("IsPublicNews").HasValue && eventItem.GetValue<bool?>("IsPublicNews").Value)
    {
        officesIds = new List<Guid>().ToArray().AsQueryable();
    }
    else
    {
        officesIds = eventItem.GetValue<TrackedList<Guid>>("Offices")?.ToArray().AsQueryable();
    }

    return officesIds;
}

...

EventsManager manager = EventsManager.GetManager();
IQueryable<Event> eventItems = manager.GetEvents();
Expression<Func<Event, bool>> officeFilter = eventItem =>
    eventItem.GetEventOfficesIds().Contains(office.Value) && !eventItem.GetEventOfficesIds().Any();
if (office != null && office.Value != Guid.Empty)
{
    eventItems = eventItems.Where(officeFilter);
}

Can I change the code of my method or OpenAccess ORM does not support executing methods written manually?

errve
  • 13
  • 3
  • Almost no LINQ provider will support something like this because code inside a method is opaque and can't be translated to the provider's logic. You'll have to split as much of the logic as you can to run directly in the query, and the rest can only be applied after an `.AsEnumerable()` to switch to LINQ to Objects (which may incur a performance penalty, as at that point all the results have to be produced). – Jeroen Mostert Nov 29 '20 at 20:34
  • Q: Are you actually using OpenAccess ORM (the old name) or Data Access? Q: Would you consider switching to Entity Framework? – paulsm4 Nov 29 '20 at 20:34
  • @paulsm4 I use OpenAccess in my app. Version: 2018.0.1127.1. Switching to the Entity Framework is not currently considered. – errve Nov 29 '20 at 20:51

1 Answers1

0

I would simplify it to something like this:

using Telerik.Sitefinity.Data.Linq.Dynamic;

EventsManager manager = EventsManager.GetManager();
IQueryable<Event> eventItems = manager.GetEvents();

if (office != null && office.Value != Guid.Empty)
{
    eventItems = eventItems
                   .Where("IsPublicNews = true")
                   .Where(e => e.GetValue<TrackedList<Guid>>("Offices")?.ToArray().Contains(office.Value))
}
Veselin Vasilev
  • 3,698
  • 1
  • 15
  • 21