前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >RavenDb学习(五)结果转换

RavenDb学习(五)结果转换

作者头像
岑玉海
发布2018-03-01 15:02:20
5890
发布2018-03-01 15:02:20
举报
文章被收录于专栏:岑玉海
代码语言:javascript
复制
1)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");

2) 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 > 100)
                                         .OfType<ProductViewModel>()
                                         .ToList();
3)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 > 50)
                                         .ProjectFromIndexFieldsInto<ProductViewModel>()
                                         .ToList();

4)客户端转换


索引的定义:
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 =>
        {
            double count = 0;
            int totalSum = 0;
 
            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();
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2014-02-01 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档