locked
HowTo: Add default filter to a linq-Statement. RRS feed

  • Question

  • Hy,

    i have the following problem to solve:

    The user logs into the application and thereby he select a mandator.

    Now i would like to show him only the datarecords for the choosen madator.

    maybe this linq statement:

    var articles = Context.Articles;

    should be rewritten to:

    Context.Articles.Where(item => item.Madator == LogInMandator);

    But i dont want this be done by the module-developer. my wish is that it could be done in the background for every database query.

     

    Does somebody know a possible solution for that?

     

    Thanks in advance

    Tuesday, July 19, 2011 9:01 AM

Answers

  • Hello,

    there is no built in solution for that because EF doesn't have this king of functionality. If you want such functionality do not expose Context.Articles and instead expose custom IQueryable or ObjectQuery which will already contains this filter. That generally means wrapping your context into another class and it leads to repository. Anyway this will work only for direct queries. It will not work for filtering eagerly loaded (by calling Include) or lazily loaded data (because neither of them can be filtered).

    Best regards,
    Ladisalv

    Tuesday, July 19, 2011 11:53 AM