Question

I have the following two class definitions:

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; }
}

and

public class IpRange
{
    public long Start { get; set; }
    public long End { get; set; }
}

I have an index defined as follows:

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
            };
    }
}

I then try to query the index as follows:

var queryable = DocumentSession.Query<IpRange, Locations_ByRange>()
                        .FirstOrDefault(x => x.Start <= reverseIp && x.End >= reverseIp) ?? new IpRange();

But, I am met with the following error when running the query:

Exception Details: System.InvalidCastException: Unable to cast object of type 'Jodolo.Data.Locations.Location' to type 'Jodolo.Data.Locations.IpRange'.

And, here is the 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

. . .

I am somewhat pleased to see this, because ultimately I am trying to retrieve the Location document that satisfies the query parameters. It's clear that I can change the type declaration on the query call to

.Query<Location, Locations_ByRange>()

But, I then have trouble figuring out how to query the fields stored in the index; since there are no Start and End fields defined for a Location object.

Any help with this would be greatly appreciated.

Was it helpful?

Solution

Change your query to:

using Raven.Client;

...

var location = session.Query<IpRange, Locations_ByRange>()
                      .Where(x => x.Start <= reverseIp && x.End >= reverseIp)
                      .As<Location>()
                      .FirstOrDefault();

In your index, you may need to add sorting hints:

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);
    }
}

Note that I added the return type at the top in AbstractIndexCreationTask<Location, IpRange>. This will align the types so the Sort method can locate those properties.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top