我正在尝试构造一个包含多个||的LINQ WHERE子句(或)?
所以我有一个EmployeeObject
(列表)的集合,每个EmployeeObject
对象的集合都是Department
,我想查询此EmployeeObject
的集合Department
这是我的代码:
public static IQueryable<EmployeeObject> QueryableSQL()
{
IQueryable<EmployeeObject> queryable = EmployeeRepository.GetAllEmployee();
return queryable;
}
public static IList<EmployeeObject> QLike(Expression<Func<EmployeeObject, bool>> func)
{
return QueryableSQL().Where(func).ToList();
}
EmployeeObject结构:
public int Id {get; set;}
public string Name {get; set;}
.....
public IList<Departments> EmployeeDept {get; set;}
系:
public int DeptId {get; set;}
public string DeptName {get; set;}
这是我到目前为止所尝试的内容:
string[] array = { "stack", "overflow"};
bool result0 = QLike(t => t.EmployeeDept.Any(x => array.Contains(x.DeptName)));
OR
bool result1 = QLike((t => t.EmployeeDept.Any(x => x.DeptName.IndexOf(array[0], StringComparison.OrdinalIgnoreCase) >= 0 || t.EmployeeDept.Any(y => y.DeptName.IndexOf(array[1], StringComparison.OrdinalIgnoreCase) >= 0))));
OR
bool result2 = QLike((a => a.EmployeeDept.Any(q => q.DeptName.Contains(array[0])) || a.EmployeeDept.Any(a1 => a1.DeptName.Contains(array[1]))));
PS:我尝试了不同的方法但没有返回任何行,我的数据库中确实有数据。
答案 0 :(得分:0)
这可能是因为Departments没有在原始查询中填充。
public static IList<EmployeeObject> QLike(Expression<Func<EmployeeObject, bool>> func)
{
var queryable = QueryableSQL().Where(func);
return queryable.ToList();
}
尝试调试可查询变量,您将看到如何构成可查询。要使它包含Departments,您可以关注Tony注释Include(“EmployeeDept”),但它仍然取决于您的ORM框架。