Skip to content

Instantly share code, notes, and snippets.

@mattjohnsonpint
Created January 13, 2014 04:03
Show Gist options
  • Select an option

  • Save mattjohnsonpint/8394515 to your computer and use it in GitHub Desktop.

Select an option

Save mattjohnsonpint/8394515 to your computer and use it in GitHub Desktop.
Test for SO Q21083222
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using Raven.Abstractions.Indexing;
using Raven.Client;
using Raven.Client.Indexes;
using Raven.Imports.Newtonsoft.Json;
using Raven.Tests.Helpers;
using Xunit;
namespace RavenTests
{
public class Q21083222 : RavenTestBase
{
[Fact]
public void Test()
{
using (var documentStore = NewDocumentStore())
{
documentStore.ExecuteIndex(new ShippedItemsIndex());
using (var session = documentStore.OpenSession())
{
session.Store(new Order
{
OrderId = 1,
CustomerName = "Joe",
OrderDate = new DateTime(2013, 1, 1),
OrderLines = new List<OrderLine>
{
new OrderLine
{
ProductName = "Foo",
Quantity = 1,
ShipDate = DateTime.Now
}
}
});
session.SaveChanges();
}
WaitForIndexing(documentStore);
using (var session = documentStore.OpenSession())
{
var query = session.Query<Order, ShippedItemsIndex>()
.ProjectFromIndexFieldsInto<ShippedItemsIndex.Result>()
.OrderByDescending(x => x.ShipDate)
.Take(10);
var results = query.ToList();
Debug.WriteLine(JsonConvert.SerializeObject(results, Formatting.Indented));
}
}
}
public class OrderLine
{
public string ProductName { get; set; }
public int Quantity { get; set; }
public DateTime? ShipDate { get; set; }
}
public class Order
{
public int OrderId { get; set; }
public string CustomerName { get; set; }
public DateTime OrderDate { get; set; }
public List<OrderLine> OrderLines { get; set; }
}
public class ShippedItemsIndex : AbstractIndexCreationTask<Order, ShippedItemsIndex.Result>
{
public class Result
{
public int OrderId { get; set; }
public string CustomerName { get; set; }
public string ProductName { get; set; }
public int Quantity { get; set; }
public DateTime ShipDate { get; set; }
}
public ShippedItemsIndex()
{
Map = orders =>
from order in orders
from line in order.OrderLines
where line.ShipDate != null
select new
{
order.OrderId,
order.CustomerName,
line.ProductName,
line.Quantity,
line.ShipDate
};
StoreAllFields(FieldStorage.Yes);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment