我有以下两个类定义:
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 字段。
对此的任何帮助将不胜感激。