无法从类型为:参数的表达式中提取值
本文关键字:表达式 提取 参数 类型 | 更新日期: 2023-09-27 18:30:58
public class Objekt
{
public Foo[] FooList{ get; set; }
}
public class Foo
{
public string Value{ get; set; }
}
在我的查询 RavenDB 中,我想匹配每个在 FooList 中都有一个条目的 Objekt,该条目的字符串属性"Item"以字符串表中的任何字符串开头。
tableString 是一个字符串 []。
var query = session.Query<Objekt>();
query = query.Where(x=> tableStrings.Any(y => x.FooList.Any(s => s.Value.StartsWith(y))));
我有这个错误:"无法从类型:参数的表达式中提取值"
有关信息,如果我只使用 tableString 的第一项,则可以
:query = query.Where(x => x.FooList.Any(y => y.Value.StartsWith(tableStrings.First())));
你可以通过使用索引有效地做到这一点:
public class ObjektFooListIndex : AbstractIndexCreationTask<Objekt, ObjektFooListIndex.Result> {
public class Result {
public string[] Values;
}
public ObjektFooListIndex() {
Map = objekts => from objekt in objekts
select new {
Values = objekt.FooList.Select(x => x.Value).ToArray()
};
Index(x => x.Values, Raven.Abstractions.Indexing.FieldIndexing.NotAnalyzed);
}
}
现在,您可以搜索Foo.Value
字符串的内容:
var result = session.Query<ObjektFooListIndex.Result, ObjektFooListIndex>()
.Search(x => x.Values, "ein*", escapeQueryOptions: EscapeQueryOptions.AllowPostfixWildcard)
.AsProjection<Objekt>()
.ToList();
结果将是Objekt
列表。要搜索多个术语,请多次使用 Search()
。
var result = session.Query<ObjektFooListIndex.Result, ObjektFooListIndex>()
.Search(x => x.Values, "ein*", escapeQueryOptions: EscapeQueryOptions.AllowPostfixWildcard, options: SearchOptions.Or)
.Search(x => x.Values, "dr*", escapeQueryOptions: EscapeQueryOptions.AllowPostfixWildcard, options: SearchOptions.Or)
.AsProjection<Objekt>()
.ToList();
您可以根据需要轻松扩展它。
要创建索引,请使用IndexCreation.CreateIndexes()
。