RavenDB Transformation - 尝试创建对象列表时抛出异常

时间:2017-05-22 10:56:29

标签: c# ravendb

我试图运行RavenDB转换来将我的文档映射到视图模型,但我遇到了这个错误:

  

错误CS1503 - 参数1:无法转换为' AnonymousType#1'

我不确定为什么我不能在这里初始化新的OfferRate列表。如何正确地将文档模型映射到视图模型?

Offer_ViewModel.cs

public class Offer_ViewModel : AbstractTransformerCreationTask<Offer>
{
    public Offer_ViewModel()
    {
        TransformResults = offers => offers
            .Select(offer => new ViewModels.Offer
            {
                Id = offer.Id,
                MerchantId = offer.MerchantId,
                Title = offer.Title,
                OfferRates = new List<OfferRate>
                {
                  new OfferRate
                  {
                      Type = OfferRateType.Base,
                      Amount = offer.Amount,
                      Percentage = offer.Percentage
                  }  
                },
                Currency = offer.Currency
            });
    }
}

我按照地图指南查看使用变换器的模型:https://ravendb.net/articles/working-with-view-models但该示例并未包含映射聚合对象。

1 个答案:

答案 0 :(得分:0)

从上面的评论中,将集合更改为数组可以正常工作。

<强> Offer_ViewModel.cs

public class Offer_ViewModel : AbstractTransformerCreationTask<Offer>
{
    public Offer_ViewModel()
    {
        TransformResults = offers => offers
            .Select(offer => new ViewModels.Offer
            {
                Id = offer.Id,
                MerchantId = offer.MerchantId,
                Title = offer.Title,
                OfferRates = new[]
                {
                  new OfferRate
                  {
                      Type = OfferRateType.Base,
                      Amount = offer.Amount,
                      Percentage = offer.Percentage
                  }  
                },
                Currency = offer.Currency
            });
    }
}