1

public static IEnumerable<Customers> GetCustomersFunc1(string[] keywords)
2
{
3
DataClassesDataContext dc = new DataClassesDataContext();
4
5
//创建一个静态类型为Customers的参数表达式
6
ParameterExpression c = Expression.Parameter(typeof(Customers), "c");
7
8
//创建一个恒等于false的表达式,用于与下面的表达式取并集
9
Expression condition = Expression.Constant(false);
10
foreach (string keyword in keywords)
11
{
12
//该表达式用于判断一个Customers类的CompanyName属性的值是否包含了关键字keyword
13
Expression con = Expression.Call(
14
Expression.Property(c, typeof(Customers).GetProperty("CompanyName")),
15
typeof(string).GetMethod("Contains", new Type[] { typeof(string) }),
16
Expression.Constant(keyword));
17
18
//与之前的condition表达式进行逻辑或运算。
19
//如果要查找的项需要包含keywords中的所有关键字,则可使用Expression.And(con, condition)
20
//并且将Expression condition = Expression.Constant(false);
21
//改成Expression condition = Expression.Constant(true);
22
condition = Expression.Or(con, condition);
23
}
24
25
//创建一个以一个Customers类作为参数并返回bool类型的委托
26
Expression<Func<Customers, bool>> end = Expression.Lambda<Func<Customers, bool>>(condition, new ParameterExpression[] { c });
27
28
//使用刚才构建的条件进行查询
29
var result = dc.Customers.Where(end);
30
return result;
31
}
32

1

public static IEnumerable<Customers> GetCustomersFunc3(string[] keywords)
2
{
3
//这个方法其实是伪Linq,核心还是在拼接SQL语句,所以就不多解释了
4
DataClassesDataContext dc = new DataClassesDataContext();
5
string sqlQuery = "SELECT [CustomerID], [CompanyName], [ContactName], [ContactTitle], [Address], ";
6
sqlQuery += "[City], [Region], [PostalCode],[Country], [Phone], [Fax] FROM [dbo].[Customers] WHERE ";
7
foreach (string keyword in keywords)
8
{
9
sqlQuery += "([CompanyName] LIKE ‘%" + keyword + "%‘ ) OR ";
10
}
11
sqlQuery += "(1=0)";
12
return dc.ExecuteQuery<Customers>(sqlQuery);
13
}
14
15