7

当在编译时不知道术语时,是否有推荐的方法来使用 StartsWith 搜索多个术语中的每一个?

我设想这样的事情:

var searchTerms = "John Doe".Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
var query = session.Query<Person, PersonIndex>()
                   .Where(x => x.FirstName.StartsWithAnyOf(searchTerms) ||
                               x.LastName.StartsWithAnyOf(searchTerms));

该查询相当于:

var query = session.Query<Person, PersonIndex>()
                   .Where(x => x.FirstName.Starts(searchTerms[0]) ||
                               x.LastName.StartsWith(searchTerms[0]) ||
                               x.FirstName.Starts(searchTerms[1]) ||
                               x.LastName.StartsWith(searchTerms[1]));

是在运行时(PredicateBuilder 或类似)构建 LINQ 查询的答案吗?

4

2 回答 2

4

使用 LuceneQuery(可能需要使用子条款,具体取决于您在做什么):

var searchTerms = "John Doe".Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);

var query = session.Advanced.LuceneQuery<Person, PersonIndex>();
query = query.OpenSubclause(); // optional

foreach (var term in terms)
{
    query = query.WhereStartsWith("FirstName"), term).OrElse();
    query = query.WhereStartsWith("LastName"), term).OrElse();
}

query = query.WhereEquals("Id", null);
query = query.CloseSubclause(); // if OpenSubclause() was used

如果您想要强类型变量名称,请查看以下答案:https ://stackoverflow.com/a/301957/941536

于 2012-06-29T18:51:54.133 回答
0

你想做什么?您可能最好使用分析字段,然后发出 a.Search而不是 a .Where

于 2012-06-27T06:52:30.310 回答