生成过滤我的实体数据的通用函数

Generate generic function that filter my entity data

我有具有以下字段的实体(学生)

int code;
string name ;
bool active;

我想创建将字符串条件作为参数传递给它的通用函数 喜欢

"where active == 1" 

此函数将查询提到的实体。

像下面的例子

public List<Student> getdatafiltered(string condition)
{
    return context.students.where(condition).Tolist();
}

要调用函数,它将调用如下两个示例

getdatafiltered("where active = 1");

getdatafiltered("where name like'%myname%'");

您可以将 Expression<Func<Student, bool>> 传递给方法:

public List<Student> GetDataFiltered(Expression<Func<Student, bool>> condition)
{
    return context.students.Where(condition).ToList();
}

用法:

var students = GetDataFiltered(s => s.Active == 1);