How can I reuse expressions within LINQ statements?

后端 未结 4 1602
南笙
南笙 2020-12-14 17:03

I like to reuse expressions for DRY reasons, but how do I reuse the expressions within a LINQ statement?

e.g.

I have

public static class MyE         


        
4条回答
  •  星月不相逢
    2020-12-14 17:26

    If you move from the LINQ syntactic sugar it is possible:

    var goodProds = dataContext.Products.Where(MyExpressions.IsAGoodProduct());
    

    Without it, it isn't possible.

    There's nothing to stop you mixing the two styles to build a single query though.

    Example:

      var goodProds = from p in dataContext.Products
                                           .Where(MyExpressions.IsAGoodProduct())
                      group p by p.Category into g 
                      select new {Category = g.Key, ProductCount = g.Group.Count()};
    

提交回复
热议问题