Reuse of a LINQ query

前端 未结 4 529
梦谈多话
梦谈多话 2020-12-14 06:06

This is not about the reuse of a result but more the statement itself. Nor is it about an error when using var as mentioned in: LINQ to SQL: Reuse lambda expression

相关标签:
4条回答
  • 2020-12-14 06:33

    Yes, you can write a function containing the query you want to reuse, which takes and returns an IQueryable<T>

       public IQueryable<T> ContainsEmpty(IQueryable<T> query)
       {
           return query.Where(x => x.Contains(""));
       }
    

    Now you can reuse it:

       query1 = ContainsEmpty(query1);
       query2 = ContainsEmpty(another);
    
    0 讨论(0)
  • You can store it in a variable. If you are working with IQueryable then use:

    System.Linq.Expressions.Expression<Func<Foo, bool>> selector = x => x.Contains("");
    

    If you are using IEnumerable then use:

    Func<Foo, bool> selector = x => x.Contains("");
    

    And use it in your query:

    query.Where(selector);
    
    0 讨论(0)
  • 2020-12-14 06:48

    I wrote a library to address exactly this concern, it's called CLinq and you can find an implementation for the EntityFramework here: https://www.nuget.org/packages/CLinq.EntityFramework

    It allows to create query snippets and use them everywhere you in a linq query. Following the example of Hamid, create the following expression:

    System.Linq.Expressions.Expression<Func<Foo, bool>> selector = x => x.Contains("");

    You can now use this query everywhere in your linq queries like this:

    query.AsComposable().Where(o => selector.Pass(o));

    Additionally to this simple example you're also able to combine your query snippets:

    query.AsComposable().Where(o => selector.Pass(o) || anotherSelector.Pass(o));

    or even merge them together:

    query.AsComposable().Where(o => anotherSelector.Pass(selector.Pass(o)));

    There's some more features, but I think it's really helpful, so check it out :)

    0 讨论(0)
  • 2020-12-14 06:55

    It depends. There's two Where methods, Enumerable.Where and Queryable.Where. If you're applying the .Where to an IEnumerable than the first one is called, if you're applying it to an IQueryable the second one is called.

    Since Enumerable.Where takes in a Func, it isn't reusable. Since Queryable.Where takes in an expression, it is reusable. You can do so as follows:

    var x = new List<string>().AsQueryable();
    
    var query = x.Where (n => n.Contains("some string"));
    
    //Extract the lambda clause
    var expr = query.Expression;
    var methodExpr = (MethodCallExpression)expr;
    var quoteExpr = (UnaryExpression)methodExpr.Arguments[1];
    var funcExpr = (Expression<Func<string, bool>>)quoteExpr.Operand;
    

    You can then later re-apply the where expression:

    var query2 = x.Where(funcExpr);
    
    0 讨论(0)
提交回复
热议问题