我正在编写实现关键字式搜索的 LINQ 查询。换句话说,返回 Title
、Description
、Id
包含
如下
public static IQueryable<EmployeeObject> QueryableSQL()
{
IQueryable<EmployeeObject> queryable = EmployeeRepository.GetAllEmployee();
}
public static IList<EmployeeObject> QLike(string txt)
{
IList<employeeObject> _emps = QueryableSQL().Where(x => x.Title == txt).ToList();
return _emps;
}
到目前为止,还不错。但这仅处理您想要在我的案例中匹配的情况 Title
。
假设相反,我想根据“Description或
Id进行搜索,我是否必须为 Description 创建一个新方法?或者有什么方法可以创建一个
树表达式`
最佳答案
public static IQueryable<EmployeeObject> QueryableSQL()
{
IQueryable<MediaObject> queryable = EmployeeRepository.GetAllEmployee();
}
public static IList<EmployeeObject> QLike(Expression<Func<EmployeeObject, bool>> func)
{
return QueryableSQL().Where(func).ToList();
}
然后你可以这样调用它:
QLike(t => t.Title == "MyText");
关于linq - 动态构建 Linq 查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19641185/