考虑以下字符串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