如何优化此lambda表达式以使用包含单词列表的每一行的实体框架进行查询?

时间:2013-05-29 09:32:32

标签: c# entity-framework lambda

我有一些输入,例如:"Steve Brian McFistycuffs Johnson"

我希望查看我的数据库,并以任何顺序返回包含这些单词的所有记录。

我要做的是像

var matches = myContext.MyTable.Where( c => c.Name.Contains("Steve") && c.Name.Contains("Brian") && c.Name.Contains("McFistycuffs") && c.Name.Conains("Johnson"));

但是我需要动态地执行此操作,并且单词的数量可能会发生变化。

我还想确保无论我选择什么,它都不会影响实体框架创建SQL语句,例如。我想确保尽可能优化它。 这意味着在lambda中做某种foreach可能不会起作用,因为实体框架不能轻易地将它映射到SQL。

有什么建议吗?

6 个答案:

答案 0 :(得分:5)

类似的东西:

static void Main()
{
    string[] words = {"abc", "def"};
    var filter = ContainsAllWords<Person>(x => x.Name, words);
    // same as: x => x.Name.Contains("abc") && x.Name.Contains("def")

    // then:
    var rows = myContext.MyTable.Where(filter);
}
static Expression<Func<T,bool>> ContainsAllWords<T>(
    Expression<Func<T, string>> selector, string[] words)
{
    if (words == null || words.Length == 0) return x => true;

    Expression body = Expression.Call(selector.Body, "Contains", null,
        Expression.Constant(words[0], typeof(string)));
    for (int i = 1; i < words.Length; i++)
    {
        body = Expression.AndAlso(body,
            Expression.Call(selector.Body, "Contains", null,
                Expression.Constant(words[i], typeof(string))));
    }
    return Expression.Lambda<Func<T, bool>>(body, selector.Parameters);
}

答案 1 :(得分:3)

var list = new[] { "Brian", "Steve", "Johnson" };
var x = myContext.MyTable.Where(c => list.All(p => c.Name.Contains(p)))

答案 2 :(得分:2)

str = "Steve Brian McFistycuffs Johnson"

string[] strParts = str.Split(' ');

myContext.MyTable.Where(c => strParts.All(part => c.Name.contains(part )))

答案 3 :(得分:1)

var arr = new string[] {"Steve","Brian","McFistycuffs","Johnson"}; 
var matches = myContext.MyTable.Where(c =>arr.All(ar => ar==c.Name));

答案 4 :(得分:1)

以下几行可能会有什么?

string inputFull = "Steve Brian McFistycuffs Johnson";
string[] stringSeparators = new string[] {" "};

var matches = myContext.MyTable;

foreach(string input in inputFull.Split(stringSeparators, StringSplitOptions.None))
{
   matches = matches.Where(c => c.Name.Contains("input"))
}

编辑:使用上面答案中的.All()会更好。

答案 5 :(得分:0)

只是猜测..如何使用正则表达式?

(尚未测试的代码)

var input = "Steve Brian McFistycuffs Johnson";
var re = new Regex(input.Replace(" ","|"));

var matches = myContext.MyTable.Where( c => re.IsMatch(c.Name));