我在使用 Entity Framework 6 时遇到问题,但不知道如何解决。我想使用 EntityFramework 从表中读取数据。目标是使用 where 子句读取该数据,该子句从特定列中过滤数据。我要搜索的列在方法参数中指定。
一个例子:我有一张人员表
| 姓名 | 名 | 地址 | 电子邮件 |
|---|---|---|---|
| 康纳 | 布莱恩 | 纽约 | abc@abc.com |
| 施瓦辛格 | 阿诺德 | 洛杉矶 | abc@abc.com |
通常我会选择这样的数据:
public List<Person> getData(string searchTerm)
{
using (var db = new myDB())
{
return db.Person.Where(x=> x.Name == searchTerm).ToList();
}
}
Run Code Online (Sandbox Code Playgroud)
但我也希望能够灵活地处理我想要过滤的列。像这样的东西:
public getData(string columnToSearch, string searchTerm)
{
using (var db = new myDB())
{
return db.Person.Where(columnToSearch == searchTerm).ToList();
}
}
Run Code Online (Sandbox Code Playgroud)
我怎样才能做到这一点?
我不想使用纯 SQL 并且无法编辑数据库。
感谢您的帮助。