1
我有以下兩個類定義:ravendb查詢locationsbyip指數
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'.
而且,這裏是堆棧跟蹤:
[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
。 。 。
我很高興看到這一點,因爲最終我試圖檢索滿足查詢參數的位置文檔。很明顯,我可以在查詢電話變更類型聲明
.Query<Location, Locations_ByRange>()
但是,我再有麻煩搞清楚如何查詢存儲在索引的字段;因爲沒有爲位置對象定義開始和結束字段。
任何幫助,這將不勝感激。
我無法構建,如果我只是更改您指定的查詢。是否有需要添加到課程中的使用說明? –
我已經爲Raven.Client.Linq添加了使用指令,但仍然沒有運氣。嗯... –
請參閱我的更新答案如何修改索引添加排序提示。沒有它們,這些字段可能會按字符串順序進行排序並拋出範圍查詢。 –