我已经让这个类像分页一样检索数据表的一部分,一切正常,但问题是该方法只能按一列过滤,我不知道如何使用 LINQ 归档它。我想传递要过滤的 n 列和 n 表达式。
示例:列:姓名,表达式“jonh” 列:城市,表达式“佛罗里达”
这是我的课:
public class Paging
{
public enum Operator
{
Equal,
Like
}
public static DataTable Page(DataTable dataTableToPage, int pageSize, int pageNumber, string columnToFilter, string expression, out int totalrecords, Operator Operator)
{
int skip = (pageNumber - 1) * pageSize;
IEnumerable<DataRow> query = null;
if (Operator == Operator.Equal)
{
query = (from dr in dataTableToPage.AsEnumerable()
where dr[columnToFilter].ToString() == expression
select dr);
}
else if(Operator == Operator.Like)
{
query = (from dr in dataTableToPage.AsEnumerable()
where dr[columnToFilter].ToString().IndexOf(expression, StringComparison.OrdinalIgnoreCase) >= 0
select dr);
}
var queryConverted = query.Skip(skip).Take(pageSize);
if (queryConverted.Count() > 0)
{
totalrecords = query.Count();
return queryConverted.CopyToDataTable();
}
totalrecords = 0;
return new DataTable();
}
}