Mon*_*kar 1 c# entity-framework
我使用 EF v5/6。我想知道当用户尝试通过 UI 动态添加搜索过滤器时,如何动态构建 where 子句。
现在最终用户将通过上述 UI 添加尽可能多的过滤器并尝试搜索数据库。
我尝试在 google 上搜索一些解决方案,结果我可以使用 EF 在我的数据库表中添加多个搜索,但我发现的很少。这里有几个链接
上面的指导方针对我来说很难理解。所以我正在寻找一些简单的东西。
这个链接有点接近我的要求 /sf/answers/1165236901/ 但我仍然不知道如何自定义他们的代码以用于我的场景。
所以我的要求是任何人都可以提供一个小的示例代码来帮助我完成任务。
我假设您能够从 GUI 中的每一行构建一个子条件。例如,第一行类似于
string userInputName = "john";
Expression<Func<Person, bool>> condition1 = person => person.Name.Contains(userInputName);
Run Code Online (Sandbox Code Playgroud)
并且可以用作
var selection = db.Persons.Where(condition1).ToList();
Run Code Online (Sandbox Code Playgroud)
对于多个子条件,and只是Where条件的串联:
var conditions = new List<Expression<Func<Person, bool>>>() { condition1, condition2, ... };
IQueryable<Person> query = db.Persons;
foreach (var condition in conditions)
{
query = query.Where(condition);
}
var selection = query.ToList();
Run Code Online (Sandbox Code Playgroud)
更棘手的部分是or条件。假设您已将条件标准化为多组替代and条件(析取范式),那么您将获得多组有效结果。为简单起见,我在这里将其保留为 2 组,但可以将其概括为与and条件相同的方式:
所述or可以通过被表示Union在子查询操作。
var conditions1 = new List<Expression<Func<Person, bool>>>() { condition1, condition2, ... };
IQueryable<Person> query1 = // see above construction logic for 'and' conditions
var conditions2 = new List<Expression<Func<Person, bool>>>() { condition5, condition6, ... };
IQueryable<Person> query2 = // see above construction logic for 'and' conditions
IQueryable<Person> query = query1.Union(query2);
var selection = query.ToList();
Run Code Online (Sandbox Code Playgroud)
最后几句话:考虑使用一些已建立的过滤器/搜索框架。这种方法可能既不是您能找到的最好的方法,也不是您能找到的最快的方法。
根据要求,一个带有一些内存数据的小例子。请注意,这不是 100% 等效于 Linq-to-entities。例如,字符串比较会以不同的方式处理大小写,并且可能不是 SQL 允许所有类型的条件。
public enum TypeOfContact
{
Unknown,
Email
}
public class Person
{
public string Name { get; set; }
public DateTime Birth { get; set; }
public TypeOfContact ContactType { get; set; }
public string ContactValue { get; set; }
}
public class Program
{
static void Main(string[] args)
{
// test data to simulate a database table
var Persons = new List<Person>
{
// + All conditions met
new Person { Name = "john doe", Birth = new DateTime(2011, 1, 1), ContactType = TypeOfContact.Email, ContactValue = "john.doe@email.com" },
// - Not in result
new Person { Name = "danny doe", Birth = new DateTime(2012, 1, 1), ContactType = TypeOfContact.Email, ContactValue = "johndoe@hotmail.com" },
// + Name contains john
new Person { Name = "john doe", Birth = new DateTime(2013, 1, 1), ContactType = TypeOfContact.Unknown, ContactValue = "" },
// + Birth, ContactType and ContactValue correct
new Person { Name = "justin", Birth = new DateTime(2014, 1, 1), ContactType = TypeOfContact.Email, ContactValue = "slenderman@email.com" },
// - Not in result because Name and Birth are wrong
new Person { Name = "jonny", Birth = new DateTime(1979, 1, 1), ContactType = TypeOfContact.Email, ContactValue = "you-know-who@email.com" },
// - Not in result
new Person { Name = "jenny doe", Birth = new DateTime(2016, 1, 1), ContactType = TypeOfContact.Unknown, ContactValue = "" },
}.AsQueryable();
// single-line-conditions
Expression<Func<Person, bool>> c1 = p => p.Name.Contains("john");
Expression<Func<Person, bool>> c2 = p => p.Birth.Date >= new DateTime(1980, 1, 1);
Expression<Func<Person, bool>> c3 = p => p.ContactType == TypeOfContact.Email;
Expression<Func<Person, bool>> c4 = p => p.ContactValue.EndsWith("@email.com");
// DNF groups: outer list = or; inner list = and
// c1 or (c2 and c3 and c4)
var conditionList = new List<List<Expression<Func<Person, bool>>>>
{
new List<Expression<Func<Person, bool>>>
{
c1,
},
new List<Expression<Func<Person, bool>>>
{
c2,
c3,
c4,
},
};
var andSubResults = new List<IQueryable<Person>>();
foreach (var andQueries in conditionList)
{
var subQuery = Persons;
foreach (var andQuery in andQueries)
{
subQuery = subQuery.Where(andQuery);
}
andSubResults.Add(subQuery);
}
var query = andSubResults.FirstOrDefault();
foreach (var subResult in andSubResults.Skip(1))
{
query = query.Union(subResult);
}
var selection = query.ToList();
// just check the result in debugger
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2372 次 |
| 最近记录: |