我可以将Where子句注入IQueryable <t>吗?

时间:2017-07-19 17:12:38

标签: c# .net linq unit-testing

我有一个类有

的方法
public IEnumerable<string> ProductsStaleResults()
{
    using(var context = DataContext.FromID(Id))
    {
        return 
        (from change in context.ProductChangeTrackings
         join product in context.Products
         on change.SKU equals product.SKU
         where product.Status == ProductStatus.Active
         from rule in context.ProductDataQualityRules
         where rule.Status == "Active"
         &&
         (
             from result in context.ProductDataQualityResults
             where result.ProductDataQualityRuleID == rule.ProductDataQualityRuleID
                 && result.SKU == change.SKU
             select result.ProductRowVersion
         ).Any(prv => prv == change.RowVersion) == false
         select change.SKU).Distinct();
    }
}

问题是我需要运行一个集成测试,只过滤context.ProductDataQualityRules中的某些实体。理想情况下,不更改此类本身(可能会扩展它)。

所以我想以某种方式注入一个Func<ProductDataQualityRule, bool> filter以便

from rule in context.ProductDataQualityRules
where rule.Status == "Active"

表现得像

from rule in context.ProductDataQualityRules
where rule.Status == "Active" && filter(rule)

这是可能的,如果是的话,怎么样?

0 个答案:

没有答案