RavenDB TransformResult:将文档的属性分配给集合的每个元素

时间:2016-01-13 12:01:56

标签: ravendb transformation

我想将一些RavenDB文档转换为更适合UI的视图模型。 ContractorModel对象应如下所示:

public class ContractorModel
{
   public string Identifier {get; set;}
   public TrustcenterModel[] {get; set; }
}

public class TrustcenterModel
{
   public string Ean {get; set;}
   public string Name {get; set;}
}

相应的json文档采用以下格式:

ContractorJson:
{
  "Identifier": "42",
  "Trustcenters": [
    {
      "Ean": "2222222222222",
      "ValidFrom": "2016-01-13T00:00:00.0000000",
      "ValidTo": "2499-12-31T00:00:00.0000000"
    }
  ]
}
TrustcenterJson:
{
  "Ean": "2222222222222",
  "Name": "FooBar",
}

当我使用后续的Transformer查询Contractor时,我已经获得了相关的Trustcenter文档,但我缺少两个属性ValidFrom和ValidTo。如何将这两个属性从承包商投射到每个信任中心?

this.TransformResults = contractors =>
                from contractor in contractors
                select new
                {
                    contractor.Identifier,
                    Trustcenters = contractor.Trustcenters.Select(x =>  this.LoadDocument<TrustCenter>(TrustCenter.IdPrefix + x.Ean)),
                };

1 个答案:

答案 0 :(得分:1)

它应该是这样的:

this.TransformResults = contractors =>
                from contractor in contractors
                select new
                {
                    contractor.Identifier,
                    Trustcenters = contractor.Trustcenters.Select(x => new
                    {
                        x.Ean,
                        x.ValidFrom,
                        x.ValidTo,
                        Name = this.LoadDocument<TrustCenter>(TrustCenter.IdPrefix + x.Ean).Name
                    }),
                };