Meh*_*hdi 1 c# entity-framework-core .net-core
如果我必须在许多查询中使用相同的条件,是否有办法编写类似表达式的内容并在所有查询中使用它?
例如:
查询1:
Context.Products.Where(p => p.active && !p.deleted && !p.hidden && //other conditions)
Run Code Online (Sandbox Code Playgroud)
查询2:
Context.Products.Where(p => p.active && !p.deleted && !p.hidden && //other conditions)
Run Code Online (Sandbox Code Playgroud)
查询3:
Context.Products.Where(p => p.active && !p.deleted && !p.hidden && //other conditions)
Run Code Online (Sandbox Code Playgroud)
所有查询中都使用重复的条件:
p.active && !p.deleted && !p.hidden
那么,如果我可以将它们编写一次并用于每个查询,例如:
条件(方法、表达式或...)= p.active && !p.deleted && !p.hidden
Context.Products.Where(p => 条件 && //其他条件)
任何想法?
您可以将其提取到一个函数中。
GetActiveProducts(Context.Products).Where(p => ...)
public static IQueryable<Product> GetActiveProducts(IQueryable<Product> products) =>
products.Where(p => p.active && !p.deleted && !p.hidden);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
78 次 |
| 最近记录: |