)Result Transformers

public class Order
{
    public DateTime OrderedAt { get; set; }

    public Status Status { get; set; }

    public string CustomerId { get; set; }

    public IList<OrderLine> Lines { get; set; }
}

//单独使用
public class OrderStatisticsTransformer : AbstractTransformerCreationTask<Order>
{
    public OrderStatisticsTransformer()
    {
        TransformResults = orders => from order in orders
                                     select new
                                            {
                                                order.OrderedAt,
                                                order.Status,
                                                order.CustomerId,
                                                CustomerName = LoadDocument<Customer>(order.CustomerId).Name,
                                                LinesCount = order.Lines.Count
                                            };
    }
}

public class OrderStatistics
{
    public DateTime OrderedAt { get; set; }

    public Status Status { get; set; }

    public string CustomerId { get; set; }

    public string CustomerName { get; set; }

    public int LinesCount { get; set; }
}

//配合查询使用
IList<OrderStatistics> statistics = session.Query<Order>()
    .TransformWith<OrderStatisticsTransformer, OrderStatistics>()
    .Where(x => x.CustomerId == "customers/1")
    .ToList();

OrderStatistics statistic = session.Load<OrderStatisticsTransformer, OrderStatistics>("orders/1");

) OfType<T>
 OfType<T>是在客户端进行数据转换
比如说我们有一个这样的索引:
public class Product_ByQuantity : AbstractIndexCreationTask<Product>
{
    public Product_ByQuantity()
    {
        Map = products => from product in products
                          select new
                          {
                              QuantityInWarehouse = product.QuantityInWarehouse
                          };

        TransformResults = (database, results) => from r in results
                                                  select new
                                                  {
                                                      Name = r.Name,
                                                      Description = r.Description
                                                  };
    }
}

产品类:
public class Product
{
    public string Id { get; set; }
    public string ArticleNumber { get; set; }
    public string Name { get; set; }
    public string Manufacturer { get; set; }
    public string Description { get; set; }
    public int QuantityInWarehouse { get; set; }
}

返回结果类:
public class ProductViewModel
{
    public string Name { get; set; }
    public string Description { get; set; }
}

查询的之后,进行转换
List<ProductViewModel> products = session.Query<Product, Product_ByQuantity>()
                                         .Where(x => x.QuantityInWarehouse > )
                                         .OfType<ProductViewModel>()
                                         .ToList();
)ProjectFromIndexFieldsInto

这是map-only的索引

public class Product_ByQuantityNameAndDescription : AbstractIndexCreationTask<Product>
{
    public Product_ByQuantityNameAndDescription()
    {
        Map = products => from product in products
                          select new
                          {
                              QuantityInWarehouse = product.QuantityInWarehouse,
                              Name = product.Name,
                              Description = product.Description
                          };

        Stores.Add(x => x.Name, FieldStorage.Yes);
        Stores.Add(x => x.Description, FieldStorage.Yes);
    }
}

//查询
List<ProductViewModel> products = session.Query<Product, Product_ByQuantityNameAndDescription>()
                                         .Where(x => x.QuantityInWarehouse > )
                                         .ProjectFromIndexFieldsInto<ProductViewModel>()
                                         .ToList();

)客户端转换

索引的定义:
public class Product_ById : AbstractIndexCreationTask<ProductItem>
{
    public Product_ById()
    {
        Map = products => from product in products
                          select new
                          {
                              product.Id
                          };
    }
}

var warehouses = session.Query<dynamic, Product_ById>()
    .Customize(x => x.TransformResults((query, results) =>
        results.Cast<dynamic>().GroupBy(p => p.WarehouseId).Select(g =>
        {
            ;
            ;

            var products = g.Select(product =>
            {
                count++;
                totalSum += product.Price;
                return new ProductItemViewModel
                           {
                               Name = product.Name,
                               Description = product.Description
                           };
            }).ToList();

            return new Warehouse()
                       {
                           Id = g.Key,
                           Products = products,
                           AverageProductPrice = totalSum / count,
                       };
        }))).ToList();
05-06 13:56