我有以下带有 linq 查询的代码:
class Program
{
static void Main(string[] args)
{
var allProfessionals = new Collection<Professional>
{
new Professional { Name = "Bruno Paulovich Silva" },
new Professional { Name = "Ivan Silva Paulovich Bruno"},
new Professional { Name = "Camila Campos"}
};
var namesSearch = new[] {"bruno", "silva"};
var query = namesSearch.Aggregate(allProfessionals.AsQueryable(), (current, nome) => current.Where(oh => oh.Name.ToLower().Contains(nome.ToLower())));
foreach (var res in query.ToList())
{
Console.WriteLine(res.Name.ToLower());
}
}
}
结果是:
Bruno Paulovich Silva
Silva Paulovich Bruno
我想知道如何将 linq 查询聚合转换为可以在其他时间重用的通用方法。
在下面的示例中,我展示了我对项目中使用的通用查询的理解:
public IQueryable<T> QueryBy(Expression<Func<T, bool>> criteria)
{
return DbSet.Where(criteria);
}
ps:对不起我的英语不好