title: How to query nested information in RavenDB?
body: <p>I have the following document called Reservation:</p>
  
  <pre><code>{
      "CustomerId": 1,
      "Items": [
          {
              "EmployeeId": "employees/1",
              "StartTime": "2011-08-15T07:20:00.0000000+03:00",
              "EndTime": "2011-08-15T07:40:00.0000000+03:00"
          },
          {
              "EmployeeId": "employees/1",
              "StartTime": "2011-08-15T07:40:00.0000000+03:00",
              "EndTime": "2011-08-15T09:10:00.0000000+03:00"
          },
          {
              "EmployeeId": "employees/3",
              "StartTime": "2011-08-16T07:20:00.0000000+03:00",
              "EndTime": "2011-08-16T11:35:00.0000000+03:00"
          }
      ]
      "ReservedAt": "2011-10-20T15:28:21.9941878+03:00"
  }
  </code></pre>
  
  <p>In addition I have the following projection class:</p>
  
  <pre><code>public class ReservationItemProjection
  {
      public string ReservationId { get; set; }
      public string CustomerId { get; set; }
      public string EmployeeId { get; set; }
      public DateTime StartTime { get; set; }
      public DateTime EndTime { get; set; }
  }
  </code></pre>
  
  <p>What kind of index and query do I write if I want to find matching
  ReservationItemProjections? E.g.:</p>
  
  <pre><code>// invalid example query:
  var matches = docs.Query&lt;ReservationItemProjection,
      ReservationItemProjectionsIndex&gt;()
      .Where(x =&gt;
          x.EmployeeId == "employees/1" &amp;&amp;
          x.StartTime &gt;= minTime &amp;&amp;
          x.EndTime &lt;= maxTime)
      .ToList();
  </code></pre>
  
  <p>Please note, that I do not wish to get a list of Reservation documents but a
  list of ReservationItemProjection objects. The <a href="http://ravendb.net/documentation/docs-http-indexes-querying" rel="noreferrer">documentation</a> says:</p>
  
  <blockquote>
    <p>But while just getting the documents matching a particular query is useful, we can do better than that. Instead of getting the documents themselves, I want to get the values directly from the index, without getting the full document.</p>
  </blockquote>
  
  <p>I have already tried using an index like this:</p>
  
  <pre><code>public class ReservationItemProjectionsIndex : 
      AbstractIndexCreationTask&lt;Reservation, ReservationItemProjection&gt;
  {
      public ReservationItemProjectionsIndex()
      {
          Map = reservations =&gt; 
              from reservation in reservations
              from item in reservation.Items
              select new
              {
                  ReservationId = reservation.Id,
                  CustomerId = reservation.CustomerId,
                  item.EmployeeId,
                  item.StartTime,
                  item.EndTime
              };
          Store(x =&gt; x.ReservationId, FieldStorage.Yes);
          Store(x =&gt; x.CustomerId, FieldStorage.Yes);
          Store(x =&gt; x.EmployeeId, FieldStorage.Yes);
          Store(x =&gt; x.StartTime, FieldStorage.Yes);
          Store(x =&gt; x.EndTime, FieldStorage.Yes);
      }
  }
  </code></pre>
  
  <p>Somehow I can't get the query and index working: it either throws an exception about
  not being able to cast from ReservationItemProjection to Reservation or, when I
  have been able to get the ReservationItemProjection objects, they will have
  included all Items in all Reservations that have even one matching Item, even
  though my query has the Where-clause x.EmployeeId == "employees/1".</p>
  
  <p><strong>Summary:</strong> what is the required index? Does the index need only a Map clause or also Reduce or TransformResults? How do I write the query in C#?</p>