Linq to entity with dynamically growing where 子句
Linq to entity with dynamically growing where clause
我正在尝试根据某些条件从实体中获取数据。 WHERE 子句将有任意数量的 'OR' 条件。我怎样才能做到这一点?
var skill = skillset.split(";"); //array can have any number of items
var EmployeeList1 = EmployeeList
.Where(x => x.Primary1.ToUpper().Contains(skill[0])
|| x.Primary2.ToUpper().Contains(skill[0])
|| x.Primary1.ToUpper().Contains(skill[1])
|| x.Primary2.ToUpper().Contains(skill[1]))
.ToList();
return EmpMapper.Mapper.Map<List<EmployeeModel>>(EmployeeList1);
我需要为具有 'n' 个 'OR' 条件的 where 子句构建一个表达式。我尝试使用谓词,但出现异常(索引超出数组范围。)。
var predicate = PredicateBuilder.New<ResourceEntity>(true);
for (int i = 0; i < skill.Count(); i++)
{
predicate= predicate.Or(p => p.Primary1.Contains(skill[i]));
}
您必须将 for
变量复制到局部变量中:
var predicate = PredicateBuilder.New<ResourceEntity>(true);
for (int i = 0; i < skill.Length; i++)
{
var s = skill[i];
predicate = predicate.Or(p => p.Primary1.ToUpper().Contains(s));
predicate = predicate.Or(p => p.Primary2.ToUpper().Contains(s));
}
我正在尝试根据某些条件从实体中获取数据。 WHERE 子句将有任意数量的 'OR' 条件。我怎样才能做到这一点?
var skill = skillset.split(";"); //array can have any number of items
var EmployeeList1 = EmployeeList
.Where(x => x.Primary1.ToUpper().Contains(skill[0])
|| x.Primary2.ToUpper().Contains(skill[0])
|| x.Primary1.ToUpper().Contains(skill[1])
|| x.Primary2.ToUpper().Contains(skill[1]))
.ToList();
return EmpMapper.Mapper.Map<List<EmployeeModel>>(EmployeeList1);
我需要为具有 'n' 个 'OR' 条件的 where 子句构建一个表达式。我尝试使用谓词,但出现异常(索引超出数组范围。)。
var predicate = PredicateBuilder.New<ResourceEntity>(true);
for (int i = 0; i < skill.Count(); i++)
{
predicate= predicate.Or(p => p.Primary1.Contains(skill[i]));
}
您必须将 for
变量复制到局部变量中:
var predicate = PredicateBuilder.New<ResourceEntity>(true);
for (int i = 0; i < skill.Length; i++)
{
var s = skill[i];
predicate = predicate.Or(p => p.Primary1.ToUpper().Contains(s));
predicate = predicate.Or(p => p.Primary2.ToUpper().Contains(s));
}