Thanks Ayende.
I did not know about SelectFields. Is it the same as fetch on http API
for indexes?
The problem is still there. I am storing all the fields. I tried the
query with all the combinations of As<>, SelectFields, and Query<,>
but I still get the same exception: InvalidCastException: Unable to
cast object of type 'ConsoleApplication1.Person' to type
'ConsoleApplication1.PersonCar'.
You can find the complete source code below. I would really appreciate
if you could help solve this problem. My apologies for the trouble.
using System;
using System.Collections.Generic;
using System.Linq;
using Raven.Client.Client;
using Raven.Client.Indexes;
using Raven.Database.Indexing;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
using (var store = new EmbeddableDocumentStore
{ DataDirectory = "Data"})
{
store.Initialize();
store.DocumentDatabase.Configuration.RunInMemory =
true;
IndexCreation.CreateIndexes(typeof(CarsIndex).Assembly, store);
using (var session = store.OpenSession())
{
var john = new Person { Name = "John", LastName =
"Smith" };
john.AddCar("Holden", "Commodore",
DateTime.Parse("2005-10-05"));
john.AddCar("Toyota", "Rav 4",
DateTime.Parse("2010-12-25"));
session.Store(john);
var hellen = new Person { Name = "Hellen",
LastName = "Smith" };
hellen.AddCar("Citroen", "Xantia",
DateTime.Parse("2004-10-05"));
session.Store(hellen);
session.SaveChanges();
foreach (var p in session.Query<Person>())
{
Console.WriteLine("Id: {0}, Name: {1}, Last
Name: {2}", p.Id, p.Name, p.LastName);
foreach (var car in p.Cars)
{
Console.WriteLine("Id: {0}, Make: {1},
Model: {2}", car.CarId, car.Make, car.Model);
}
}
var result = session
.Query<PersonCar, CarsIndex>()
.Customize(q =>
q.SelectFields<PersonCar>("PersonId", "PersonName", "CarId",
"CarMake"));
foreach (var r in result)
Console.WriteLine(
"Person Name: {0}, Car Id: {1}, Car Make:
{2}",
r.PersonName,
r.CarId,
r.CarMake);
}
Console.ReadLine();
}
}
}
public class Car
{
public Car()
{
CarId = Guid.NewGuid();
}
public Guid CarId { get; set; }
public string Make { get; set; }
public string Model { get; set; }
public DateTime Year { get; set; }
}
public class Person
{
public Person()
{
Id = "Persons/";
Cars = new List<Car>();
}
public void AddCar(string make, string model, DateTime year)
{
var car = new Car
{
Make = make,
Model = model,
Year = year
};
Cars.Add(car);
}
public string Id { get; set; }
public string Name { get; set; }
public string LastName { get; set; }
public List<Car> Cars { get; set; }
}
public class PersonCar
{
public string PersonId { get; set; }
public string PersonName { get; set; }
public Guid CarId { get; set; }
public string CarMake { get; set; }
}
public class CarsIndex : AbstractIndexCreationTask<Person,
PersonCar>
{
public CarsIndex()
{
Map = persons => from person in persons
from car in person.Cars
select new
{
PersonId = person.Id,
PersonName = person.Name,
car.CarId,
CarMake = car.Make
};
Stores.Add(p => p.PersonId, FieldStorage.Yes);
Stores.Add(p => p.PersonName, FieldStorage.Yes);
Stores.Add(p => p.CarId, FieldStorage.Yes);
Stores.Add(p => p.CarMake, FieldStorage.Yes);