Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在不同存储库中共享类似linq到实体表达式逻辑的推荐做法是什么_C#_Linq_Linq To Entities_Repository Pattern - Fatal编程技术网

C# 在不同存储库中共享类似linq到实体表达式逻辑的推荐做法是什么

C# 在不同存储库中共享类似linq到实体表达式逻辑的推荐做法是什么,c#,linq,linq-to-entities,repository-pattern,C#,Linq,Linq To Entities,Repository Pattern,使用repository模式时,有时不同的存储库中会出现相同的逻辑。在下面的示例中,EmployeeRepository中的GetTempEmployees(),CompanyRepository中的GetCompaniesWithTemps() 表情相同 e.IsTemp && e.IsDeleted == false 我的问题是,为了最大限度地减少表达式逻辑的重复,建议采用什么做法 例如 公共类员工 { public int EmployeeId{get;set;} 公共b

使用repository模式时,有时不同的存储库中会出现相同的逻辑。在下面的示例中,EmployeeRepository中的GetTempEmployees(),CompanyRepository中的GetCompaniesWithTemps() 表情相同

e.IsTemp && e.IsDeleted == false
我的问题是,为了最大限度地减少表达式逻辑的重复,建议采用什么做法

例如

公共类员工
{
public int EmployeeId{get;set;}
公共bool IsTemp{get;set;}
公共布尔被删除{get;set;}
}
公营公司
{
public int CompanyId{get;set;}
公共布尔被删除{get;set;}
公共虚拟ICollection雇员{get;set;}
}
公共类TestContext:DbContext
{
公共TestContext()
{
}
公共数据库集雇员{get;set;}
公共数据库集公司{get;set;}
}
公营雇员公寓
{
私有只读测试上下文_上下文;
EmployeeRepository(TestContext上下文)
{
_上下文=上下文;
}
公共ICollection GetTempEmployees()
{
返回_context.Employee.Where(e=>e.IsTemp&&e.IsDeleted==false).ToList();
}
}
公共类公司储蓄
{
私有只读测试上下文_上下文;
CompanyRepository(TestContext上下文)
{
_上下文=上下文;
}
公共ICollection GetCompanyWithTemps()
{
返回_context.Company.Where(c=>c.Employees.Any(e=>e.IsTemp&&e.IsDeleted==false)).ToList();
}
}

我认为您必须使用,例如


但我对此有点模糊,所以试试看它是否有效。

罗林提出的解决方案会奏效。另一个解决方案是在iQueryTables上提供扩展。比如:

static class PersonSetExtensions
{
    public static IQueryable<Person> WhereTempAndNotDeleted(this IQueryable<Person> set)
    {
        return set.Where(x => x.IsTemp && !x.IsDeleted);
    }
}

这是我上次回答类似问题时提出的建议:)这似乎也是一个很好的解决方案,但如何将此方法用于集合查询,例如:Any((e=>e.IsTemp&&e.IsDeleted==false)将表达式转过来!
\u context.Company.Where(x=>x.Employees.WhereTempAndNotDeleted().Any()).ToList();
在更改为x=>x.Employees.AsQueryable().WhereTempAndNotDeleted().Any()之后,我尝试了这个方法我发现错误LINQ to Entities无法识别方法'System.LINQ.IQueryable`1,因为您正在调用AsQueryable。此调用没有SQL表示,因此EF将失败。请删除此调用,因为它是不必要的。这似乎很有趣。如果在Collection上使用AsQueryable扩展,我可以尝试此方法n_context.Company.Where(c=>c.Employees.AsQueryable().Any(EmployeeExpressions.IsTempAndNotDeleted)).ToList();
static class EmployeeExpressions
{
    public static System.Linq.Expressions.Expression<Func<Employee, bool>>
        IsTempAndNotDeleted = e => e.IsTemp && !e.IsDeleted;
}
...
return _context.Employee
    .Where(EmployeeExpressions.IsTempAndNotDeleted)
    .ToList();

...
return _context.Company
    .Where(c => c.Employees.Any(EmployeeExpressions.IsTempAndNotDeleted))
    .ToList();
static class PersonSetExtensions
{
    public static IQueryable<Person> WhereTempAndNotDeleted(this IQueryable<Person> set)
    {
        return set.Where(x => x.IsTemp && !x.IsDeleted);
    }
}
return _context.Employee
               .WhereTempAndNotDeleted()
               .ToList();