我有一些非常相似的 LINQ 查询:
var city = "Hamburg";
var query1 =
from c in DB.Customers
join address in DB.Addresses on c.ID equals address.CustomerID
where address.City == city
select c;
var query2 =
from c in DB.Customers
join address in DB.Addresses on c.ID equals address.CustomerID
where address.City.StartsWith(city)
select c;
etc.
我想使用循环来创建查询以减少冗余:
var city = "Hamburg";
var expressions = new Expression<Func<string, bool>>[] {
a => a == city,
a => a.StartsWith(city)
};
for(int i = 1; i <= 2; i++) {
queries.Add(
from c in DB.Customers
join address in DB.Addresses on c.ID equals address.CustomerID
where expressions[i](address.City)
select c
);
}
但我现在不知道如何创建表达式数组。任何想法?