我有一些非常相似的LINQ查詢:同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
);
}
但我不現在不知道如何創建表達式數組。任何想法?
老實說,這看起來像存儲過程的工作。 – asawyer
您的預期成果是什麼?一個清單,「我」列出? – Hogan
@Hogan我期望'i'列表包含由不同表達式過濾的客戶。 – Dennis