考慮以下字符串arrray:如何在一個循環中建立一個動態的LINQ表達式樹
string[] ranges = new string[]{"0-100", "100-200", "500-1000"};
我想動態地表達這種在LINQ表達式 - 沿着東西線:
var query = from p in Db.Products()
where p.Amount() >= 0
where p.Amount() <= 100
where p.Amount() >= 101
where p.Amount() <= 200
where p.Amount() >= 500
where p.Amount() <= 1000
select p;
我知道如何從數組中提取的值,所以這不是問題,但越是這樣我如何動態地構建LINQ表達式的for循環:
string[] ranges = new string[]{"0-100", "100-200", "500-1000"};
foreach (var item in ranges)
{
int min = int.Parse(item.Split('-').First());
int max = int.Parse(item.Split('-').Last());
//Linq expression?
}
注意;要非常小心,不要直接在任何表達式樹中使用'item'本身**;它將捕獲**變量**,而不是任何給定迭代的值。目前的方法獲得'min'和'max'等,並在查詢中使用**這些**是好的。 – 2012-03-15 11:42:37