【发布时间】:2012-03-15 23:02:47
【问题描述】:
继我提出的previous 问题之后,我现在正试图弄清楚如何为 AND 和 OR 查询构建动态表达式。
给定以下字符串数组:
string[] ranges = new string[]{"0-100", "100-200", "500-1000"};
我想在 linq 表达式中动态表达这一点 - 类似于:
var v = from p in products
where
(p.Amount >= 0 && p.Amount <= 100) ||
(p.Amount >= 101 && p.Amount <= 200) ||
(p.Amount >= 500 && p.Amount <= 1000)
select p;
如何在这个循环中动态构建 linq 表达式?
string[] ranges = new string[]{"0-100", "100-200", "500-1000"};
var query = products.AsQueryable();
foreach (var item in ranges)
{
int min = int.Parse(item.Split('-').First());
int max = int.Parse(item.Split('-').Last());
//Linq expression?
}
【问题讨论】: