【发布时间】:2011-05-12 03:29:05
【问题描述】:
我有一个包含“命令”和“状态”的数据库表。每个命令可以有多个状态,用户可以在搜索时进行配置。例如,命令可以是“运行”,它可以有两种状态:“快”和“慢”。
我想在我的表中搜索所有名为“运行”的命令,其中包含“快”或“慢”。这很简单:
var results = from t in table
where t.Command == "Run" &&
(t.State == "Fast" || t.State == "Slow")
return t;
但是,用户也可以搜索状态为“Fast”的命令“Walk”,因此查询如下所示:
var results = from t in table
where (t.Command == "Run" &&
(t.State == "Fast" || t.State == "Slow")) ||
(t.Command == "Walk" &&
t.State == "Fast")
return t;
这样的搜索有可能进行很多次,我想知道如何将它们组合成一个循环。
我做不到:
foreach(var command in commands)
{
foreach(var state in command.states)
{
results = from t in table
where t.Command == command.Command &&
t.State == state;
}
}
因为一旦搜索“Run”,“Walk”就会被排除在结果之外,因此询问“Walk”将根本没有结果。
有谁知道这样做的好方法吗?
【问题讨论】:
标签: c# asp.net-mvc linq-to-sql for-loop