Linq to SQL: How can I use sub-functions for filtering?

Viewed 55

I have a rather complex condition for my WHERE query in the database. If I were to use straight SQL I probably would put that part of the SQL query into a named variable to signify its semantic purpose.

If I were to use straight Linq I would use a function-call that matches the Where() predicate where the function name would tell me the purpose of the filter.

Unfortunately one can't seem to use sub-functions in Linq to SQL because then the query cannot be resolvd to SQL.

Is there a way to achieve this?

Here is the original Linq-to-SQL query:

query = query.Where(
    f => f.Table1.Any(
        t1 =>
            (t1.Table2.Any(t2 => t2.Header == Decoder.Identifier
                && DbFunctions.Like(t2.Content, pattern)))
            ||
            (!t1.Table2.Any(t2 => t2.Header == Decoder.Identifier)
                && DbFunctions.Like(t1.Text, pattern))
        ));

What I would like is something like this:

query = query.Where(
    f => f.Table1.Any(
        t1 =>
            HasCondition1(t1, pattern)
            ||
            HasCondition2(t1, pattern)
        ));
0 Answers
Related