LINQ Query Performance Problem

I have the following LINQ expression in method syntax

IEnumerable<PageElement> elements_test = ObjectContext.PageElements
            .Where(_dateDebutCheck).Where(_dateFinCheck)
            .Where(pe => _activeLanguageCheck(pe, language))
            .Where(pe => _typeCheck(pe, typeElement))
IList<PageElement> list = elements_test.ToList();

private readonly Func<PageElement, bool> _dateDebutCheck = pe => pe.DateDebut.HasValue && pe.DateDebut.Value <= DateTime.Now;
private readonly Func<PageElement, bool> _dateFinCheck = pe => !pe.DateFin.HasValue || pe.DateFin.Value > DateTime.Now;
private readonly Func<PageElement, byte, bool> _activeLanguageCheck = (pe, l) => pe.PageElementLanguages.Where(y => y.Active).Select(y => y.LanguageId).Contains(l);
private readonly Func<PageElement, byte?, bool> _typeCheck = (pe, t) => pe.TypeId == t;

I find that when the ToList call takes quite a lot of time, and I wonder if there is something that I am doing wrong, which leads to performance degradation. I only run ToList at this stage as a test, but only about 7000 records are returned for a few seconds. How can I improve this?

+4
source share
1 answer

, , . PageElements . , Func<>, LINQ Enumerable Queryable. , Expression<Func<>>.

, . . , , - SQL .

, :

// needs to be in static class
public static IQueryable<PageElement> TypeCheck(this IQueryable<PageElement> q, byte? typeElement){
    return q.Where(pe=>pe.TypeID == t); // also this might not work is typeElement is null
}

ObjectContext.PageElement.TypeCheck(typeElement).ToList(); // etc..
+9

All Articles