针对外部列表查询 RavenDB 索引<T>
Querying a RavenDB index against an external List<T>
我有以下 RavenDB 索引:
public class RidesByPostcode : AbstractIndexCreationTask<Ride, RidesByPostcode.IndexEntry>
{
public class IndexEntry
{
public string PostcodeFrom { get; set; }
public string PostcodeTo { get; set; }
}
public RidesByPostcode()
{
Map = rides => from doc in rides
select new
{
doc.DistanceResult.PostcodeFrom,
doc.DistanceResult.PostcodeTo
};
StoreAllFields(FieldStorage.Yes);
}
}
我还有一个代表邮政编码的字符串列表,我想获取邮政编码列表中 PostcodeFrom
的所有游乐设施:
var postcodes = new List<string> { "postcode 1", "postcode 2" };
var rides = _database.Query<RidesByPostcode.IndexEntry, RidesByPostcode>()
.Where(x => postcodes.Contains(x.PostcodeFrom))
.OfType<Ride>()
.ToList();
但是 RavenDb 当然说它无法理解 .Contains
表达式。
如何在 RavenDb 中实现这样的查询而不必在 where
子句之前调用 .ToList()
?
好的,我找到了答案:RavenDb 的 .In()
扩展方法(请参阅文档的 "Where + In" 部分)。
显然我是从外到内思考的,而不是从内到外的:)
这是最终查询:
var rides = _database.Query<RidesByPostcode.IndexEntry, RidesByPostcode>()
.Where(x => !x.IsAccepted && x.PostcodeFrom.In(postcodes))
.OfType<Ride>()
.ToList();
我有以下 RavenDB 索引:
public class RidesByPostcode : AbstractIndexCreationTask<Ride, RidesByPostcode.IndexEntry>
{
public class IndexEntry
{
public string PostcodeFrom { get; set; }
public string PostcodeTo { get; set; }
}
public RidesByPostcode()
{
Map = rides => from doc in rides
select new
{
doc.DistanceResult.PostcodeFrom,
doc.DistanceResult.PostcodeTo
};
StoreAllFields(FieldStorage.Yes);
}
}
我还有一个代表邮政编码的字符串列表,我想获取邮政编码列表中 PostcodeFrom
的所有游乐设施:
var postcodes = new List<string> { "postcode 1", "postcode 2" };
var rides = _database.Query<RidesByPostcode.IndexEntry, RidesByPostcode>()
.Where(x => postcodes.Contains(x.PostcodeFrom))
.OfType<Ride>()
.ToList();
但是 RavenDb 当然说它无法理解 .Contains
表达式。
如何在 RavenDb 中实现这样的查询而不必在 where
子句之前调用 .ToList()
?
好的,我找到了答案:RavenDb 的 .In()
扩展方法(请参阅文档的 "Where + In" 部分)。
显然我是从外到内思考的,而不是从内到外的:)
这是最终查询:
var rides = _database.Query<RidesByPostcode.IndexEntry, RidesByPostcode>()
.Where(x => !x.IsAccepted && x.PostcodeFrom.In(postcodes))
.OfType<Ride>()
.ToList();