我有以下两个类定义:
public class Location : Doc
{
public string Country { get; set; }
public string Region { get; set; }
public string City { get; set; }
public string PostalCode { get; set; }
public Double Latitude { get; set; }
public Double Longitude { get; set; }
public string MetroCode { get; set; }
public string AreaCode { get; set; }
public List<IpRange> IpRanges { get; set; }
}
和
public class IpRange
{
public long Start { get; set; }
public long End { get; set; }
}
我的索引定义如下:
public class Locations_ByRange : AbstractIndexCreationTask<Location>
{
public Locations_ByRange()
{
Map = locations =>
from location in locations
from range in location.IpRanges
select new
{
range.Start,
range.End
};
}
}
然后我尝试按如下方式查询索引:
var queryable = DocumentSession.Query<IpRange, Locations_ByRange>()
.FirstOrDefault(x => x.Start <= reverseIp && x.End >= reverseIp) ?? new IpRange();
但是,运行查询时遇到以下错误:
Exception Details: System.InvalidCastException: Unable to cast object of type 'Jodolo.Data.Locations.Location' to type 'Jodolo.Data.Locations.IpRange'.
而且,这是StackTrace:
[InvalidCastException: Unable to cast object of type 'Jodolo.Data.Locations.Location' to type 'Jodolo.Data.Locations.IpRange'.]
Raven.Client.Document.InMemoryDocumentSessionOperations.TrackEntity(String key, RavenJObject document, RavenJObject metadata, Boolean noTracking) in c:\Builds\RavenDB-Stable\Raven.Client.Lightweight\Document\InMemoryDocumentSessionOperations.cs:357
Raven.Client.Document.SessionOperations.QueryOperation.Deserialize(RavenJObject result) in c:\Builds\RavenDB-Stable\Raven.Client.Lightweight\Document\SessionOperations\QueryOperation.cs:167
System.Linq.WhereSelectListIterator`2.MoveNext() +104
System.Collections.Generic.List`1..ctor(IEnumerable`1 collection) +381
System.Linq.Enumerable.ToList(IEnumerable`1 source) +58
Raven.Client.Document.SessionOperations.QueryOperation.Complete() in c:\Builds\RavenDB-Stable\Raven.Client.Lightweight\Document\SessionOperations\QueryOperation.cs:143
Raven.Client.Document.AbstractDocumentQuery`2.GetEnumerator() in c:\Builds\RavenDB-Stable\Raven.Client.Lightweight\Document\AbstractDocumentQuery.cs:891
System.Linq.Enumerable.FirstOrDefault(IEnumerable`1 source) +152
Raven.Client.Linq.RavenQueryProviderProcessor`1.GetQueryResult(IDocumentQuery`1 finalQuery) in c:\Builds\RavenDB-Stable\Raven.Client.Lightweight\Linq\RavenQueryProviderProcessor.cs:1529
Raven.Client.Linq.RavenQueryProviderProcessor`1.ExecuteQuery() in c:\Builds\RavenDB-Stable\Raven.Client.Lightweight\Linq\RavenQueryProviderProcessor.cs:1454
Raven.Client.Linq.RavenQueryProviderProcessor`1.Execute(Expression expression) in c:\Builds\RavenDB-Stable\Raven.Client.Lightweight\Linq\RavenQueryProviderProcessor.cs:1427
Raven.Client.Linq.RavenQueryProvider`1.Execute(Expression expression) in c:\Builds\RavenDB-Stable\Raven.Client.Lightweight\Linq\RavenQueryProvider.cs:155
Raven.Client.Linq.RavenQueryProvider`1.System.Linq.IQueryProvider.Execute(Expression expression) in c:\Builds\RavenDB-Stable\Raven.Client.Lightweight\Linq\RavenQueryProvider.cs:198
System.Linq.Queryable.FirstOrDefault(IQueryable`1 source, Expression`1 predicate) +287
。 。
我很高兴看到这一点,因为最终我试图检索满足查询参数的Location文档。很明显,我可以将查询调用的类型声明更改为
.Query<Location, Locations_ByRange>()
但是,我无法弄清楚如何查询存储在索引中的字段;因为没有为Location对象定义Start和End字段。
对此的任何帮助将不胜感激。
答案 0 :(得分:1)
将您的查询更改为:
using Raven.Client;
...
var location = session.Query<IpRange, Locations_ByRange>()
.Where(x => x.Start <= reverseIp && x.End >= reverseIp)
.As<Location>()
.FirstOrDefault();
在索引中,您可能需要添加排序提示:
public class Locations_ByRange : AbstractIndexCreationTask<Location, IpRange>
{
public Locations_ByRange()
{
Map = locations =>
from location in locations
from range in location.IpRanges
select new
{
range.Start,
range.End
};
Sort(x => x.Start, SortOptions.Long);
Sort(x => x.End, SortOptions.Long);
}
}
请注意,我在AbstractIndexCreationTask<Location, IpRange>
的顶部添加了返回类型。这将对齐类型,以便Sort
方法可以找到这些属性。