我正在尝试使用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; }
}
信息类分别只是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();
答案 0 :(得分: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的约定有点奇怪。通常的惯例是订单,客户,企业家。将您的文档视为实体本身的持久形式。您正在使用的约定将起作用,它不是通常使用的约定。