简体   繁体   English

使用linq-to-sql帮助的高级数据库搜索

[英]Advanced database search using linq-to-sql help

I have a database table that contains "commands" and "states". 我有一个包含“命令”和“状态”的数据库表。 Each command can have several states, and the user is able to configure this when search. 每个命令可以具有几种状态,并且用户可以在搜索时进行配置。 For example, the command could be "Run" and it could have two states: "Fast" and "Slow". 例如,命令可以是“运行”,并且可以具有两个状态:“快速”和“慢速”。

I want to search my table for all commands called "Run" with "Fast" or "Slow". 我想在表中搜索所有带有“快速”或“慢速”的名为“运行”的命令。 This is pretty simple to do: 这很简单:

var results = from t in table
              where t.Command == "Run" &&
              (t.State == "Fast" || t.State == "Slow")
              return t;

However the user could also search command "Walk" with state "Fast", and so the query would look like: 但是,用户也可以搜索状态为“快速”的命令“步行”,因此查询如下所示:

    var results = from t in table
                  where (t.Command == "Run" &&
                        (t.State == "Fast" || t.State == "Slow")) ||
                  (t.Command == "Walk" &&
                   t.State == "Fast")
                  return t;

There is a potential for as many searches like this, and I'm wondering how to combine them in a loop of sorts. 这样的搜索量有很大的潜力,我想知道如何将它们组合成一个循环。

I can't do this: 我不能这样做:

foreach(var command in commands)
{
    foreach(var state in command.states)
    {
        results = from t in table
                  where t.Command == command.Command &&
                  t.State == state;
    }
}

because once it searches for "Run", "Walk" would be left out of results so asking for "Walk" would result in NO results at all. 因为一旦它搜索“ Run”,“ Walk”将被排除在结果之外,因此要求“ Walk”将根本没有结果。

Does anyone know of a good way to do this? 有谁知道这样做的好方法?

Use Joe Albahari's PredicateBuilder to build a predicate: 使用Joe Albahari的PredicateBuilder来构建一个谓词:

var predicate = PredicateBuilder.False<Entry>();
foreach(var command in commands)
{
    foreach(var state in command.states)
    {
        predicate = predicate.Or(p => p.Command == command.Command && p.State == state);
    }
}
var query = table.Where(predicate);

Or a more LINQ-heavy version: 或更重的LINQ版本:

var commandStates = from c in commands
                    from s in c.states
                    select new {c.Command, State = s};
var predicate = commandStates.Aggregate(
    PredicateBuilder.False<Entry>(),
    (pred, e) => pred.Or(p => p.Command == e.Command && p.State == e.state));
var query = table.Where(predicate);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM