0

我正在尝试使用 TransformResults 功能,但我无法让它工作。我不完全确定我理解这个功能,也许还有另一种方法可以解决这个问题。我想要的只是订单中的 ID 以及客户和企业家的电子邮件地址。我很高兴所有可以带我走向正确方向的提示。这是我的代码。

文档

public class OrderDocument

    public string Id {get; set }

    public EntrepreneurInfo EntrepreneurInfo { get; set; }

    public CustomerInfo CustomerInfo { get; set; }

    public OrderStatus CurrentOrderStatus { get; set; }
}

信息类

public class EntrepreneurInfo
{
    public string EntrepreneurDocumentId { get; set; }

    public string Number { get; set; }

    public string Name { get; set; }
}

public class CustomerInfo
{
    public string CustomerDocumentId { get; set; }

    public string Number { get; set; }

    public string Name { get; set; }
}

info 类分别只是 Customer 和 Entrepreneur 文档的子集。Customer 和 Entrepreneur 文档继承自具有 EmailAddress 属性的基类 (AbstractOrganizationDocument)。

我的索引

public class OrdersApprovedBroadcastingData : 
    AbstractIndexCreationTask<OrderDocument, OrdersApprovedBroadcastingData.ReduceResult>
{
    public OrdersApprovedBroadcastingData()
    {
        this.Map = docs => from d in docs 
                           where d.CurrentOrderStatus == OrderStatus.Approved 
                            select new
                            {
                                Id = d.Id,
                                CustomerId = d.CustomerInfo.CustomerDocumentId,
                                EntrepreneurId = d.EntrepreneurInfo.EntrepreneurDocumentId
                            };

        this.TransformResults = (db, orders) => from o in orders
                let customer = db.Load<CustomerDocument>(o.CustomerId)
                let entrepreneur = db.Load<EntrepreneurDocument>(o.EntrepreneurId)
                select
                    new 
                        {
                            o.Id,
                            o.CustomerId,
                            CustomerEmail = customer.EmailAddress,
                            o.EntrepreneurId,
                            EntrepreneurEmail = entrepreneur.EmailAddress
                        };
    }

    public class ReduceResult
    {
        public string Id { get; set; }

        public string CustomerId { get; set; }

        public string CustomerEmail { get; set; }

        public string EntrepreneurId { get; set; }

        public string EntrepreneurEmail { get; set; }
    }
}

如果我在 Raven Studio 中查看这个索引的结果,我会得到除 Id 之外的所有字段的空值。最后这是我的查询。

询问

        var items =
            this.documentSession.Query<OrdersApprovedBroadcastingData.ReduceResult, OrdersApprovedBroadcastingData>()
                .Select(x => new OrdersToBroadcastListItem
                    {
                        Id = x.Id,
                        CustomerEmailAddress = x.CustomerEmail,
                        EntrepreneurEmailAddress = x.EntrepreneurEmail
                    }).ToList();
4

1 回答 1

3

将您的索引更改为:

public class OrdersApprovedBroadcastingData : AbstractIndexCreationTask<OrderDocument>
{
  public OrdersApprovedBroadcastingData()
  {
    Map = docs => from d in docs
                  where d.CurrentOrderStatus == OrderStatus.Approved
                  select new
                  {
                  };

    TransformResults = (db, orders) =>
      from o in orders
      let customer = db.Load<CustomerDocument>(o.CustomerInfo.CustomerDocumentId)
      let entrepreneur = db.Load<EntrepreneurDocument>(o.EntrepreneurInfo.EntrepreneurDocumentId)
      select new
        {
          o.Id,
          CustomerEmailAddress = customer.EmailAddress,
          EntrepreneurEmailAddress = entrepreneur.EmailAddress
        };
  }
}

您的结果类可以只是投影的最终形式,您不需要中间步骤:

public class Result
{
  public string Id { get; set; }
  public string CustomerEmailAddress { get; set; }
  public string EntrepreneurEmailAddress { get; set; }
}

如果您不想,您不必将此类嵌套在索引中。不管怎样都无所谓。您可以通过以下方式查询:

var items = session.Query<Result, OrdersApprovedBroadcastingData>();

或与

var items = session.Query<OrderDocument, OrdersApprovedBroadcastingData>().As<Result>();

虽然,对于第一种方式,约定往往是嵌套结果类,所以实际上它会是

var items = session.Query<OrderDocument.Result, OrdersApprovedBroadcastingData>();

请注意,在索引图中,我根本不包括任何属性。你问的都不需要。但是,如果您想在查询中添加 Where 或 OrderBy 子句,则您可能想要过滤或排序的任何字段都应该放在其中。

最后一件事 - 您使用的 OrderDocument、CustomerDocument、EntrepreneurDocument 约定有点奇怪。通常的约定只是订单、客户、企业家。将您的文档视为实体本身的持久形式。您使用的约定将起作用,它不是通常使用的约定。

于 2012-10-10T16:07:09.217 回答