是否有一种通用的方法来加入列表并将特定值瞄准属性?

Is there a generic way to join list and aim specific values to properties?

我想找到一种以通用方式重现下面代码的方法。在我使用的软件中,我们经常像这样比较两个对象列表:

 d.FeatureViewModels = (from feature in d.FeatureViewModels
                        join pairedFeature in s.FeaturePairTransit on feature.Id equals pairedFeature.Feature.Id
                        select new FeatureViewModel
                        {
                            Id = feature.Id,
                            Name = feature.Name,
                            DisplayOrder = feature.DisplayOrder,
                            MembershipId = feature.MembershipId,
                            IsPaired = pairedFeature.IsPaired,
                            TranslationId = feature.TranslationId,
                            DisplayNameViewModel = feature.DisplayNameViewModel,
                            DescriptionViewModel = feature.DescriptionViewModel
                        }).ToList();

可以看到,需要添加的字段是IsPaired,其他字段都来自feature。这也类似于应用程序其他地方的许多对象。与其每次都复制、粘贴和调整上面的代码,我希望有一个可以重用的通用方法(如果可能的话)。为此,我需要一些帮助。

有人可以帮忙吗?

大卫

编辑

根据 Matias 的回答和评论,我认为我必须更准确:

FeaturePairTransit class 如下所示:

public class FeaturePairTransit
    {
        public Feature Feature { get; set; }
        public bool IsPaired { get; set; }
    }

此外,FeatureViewModel 中有 IsPaired 属性 我想要实现的是找到一种将 FeaturePairTransit.IsPaired 映射到 FeatureViewModel.IsPaired 并重用该模式的方法。

这里是该特定问题的完整 AutoMapper 代码:

    Mapper.CreateMap<SKUFeatureTransit, SKUFeaturePivotViewModel>()
// Will give a different level error
                    //.ForMember(d => d.FeatureViewModels.Select(x => x.IsPaired), opt => opt.MapFrom(s => s.FeaturePairTransit.Select(x => x.IsPaired)))
                    .AfterMap((s, d) => d.SKUViewModel = Mapper.Map<SKUViewModel>(s.SKU))
                    .AfterMap((s, d) =>
                    {
                        // This is mandatory because we need to instantiate every translation fields with the constructor.
                        d.FeatureViewModels = Mapper.Map<IList<FeatureViewModel>>(s.FeaturePairTransit.Select(x => x.Feature));

                        // We cannot map an individual property (IsPaired) from a different level. So we did this comparaison.
                        d.FeatureViewModels = (from feature in d.FeatureViewModels
                                               join pairedFeature in s.FeaturePairTransit on feature.Id equals pairedFeature.Feature.Id
                                               select new FeatureViewModel
                                               {
                                                   Id = feature.Id,
                                                   Name = feature.Name,
                                                   DisplayOrder = feature.DisplayOrder,
                                                   MembershipId = feature.MembershipId,
                                                   IsPaired = pairedFeature.IsPaired,
                                                   TranslationId = feature.TranslationId,
                                                   DisplayNameViewModel = feature.DisplayNameViewModel,
                                                   DescriptionViewModel = feature.DescriptionViewModel
                                               }).ToList();
                    });

然后,我看到两个选项,也许像@Matias 所说的那样,AutoMapper 可以处理,我仍在尝试弄清楚或者我可以以通用方式进行映射。有什么建议吗?

AutoMapper, AutoMapper, AutoMapper...

你可以把你的代码变成:

 d.FeatureViewModels = (from feature in d.FeatureViewModels
                        join pairedFeature in s.FeaturePairTransit on feature.Id equals pairedFeature.Feature.Id
                        select mapper.Map<Feature, FeatureViewModel>(feautre)).ToList();

@devuxer 在一些评论中说:

Yes, but not quite that simple. You also need a CreateMap statement.

好的,因为看起来视图模型和域对象具有相同的属性....您需要调用...:[=​​15=]

var mapperConfig = new MapperConfiguration(cfg => {
    cfg.CreateMap<Feature, FeatureViewModel>();
});

var mapper = mapperConfig.CreateMapper();

...在调用 mapper.Map(...)!

之前

我终于找到了我要找的东西。

这并不像我想象的那样通用,但这是朝着正确方向迈出的良好一步。

// HTTPGET
            Mapper.CreateMap<SKUFeatureTransit, SKUFeaturePivotViewModel>()
                .AfterMap((s, d) => d.SKUViewModel = Mapper.Map<SKUViewModel>(s.SKU))
                .AfterMap((s, d) =>
                {
                    // This is mandatory because we need to instantiate every translation fields with the constructor.
                    d.FeatureViewModels = Mapper.Map<IList<FeatureViewModel>>(s.FeaturePairTransit.Select(x => x.Feature));

                    // We cannot map an individual property (IsPaired) from a different level. So we did this comparaison.
                    d.FeatureViewModels = d.FeatureViewModels.Join(s.FeaturePairTransit, vModel => vModel.Id, source => source.Feature.Id, (dest, source) =>
                    {
                        dest.IsPaired = source.IsPaired;
                        return dest;
                    }).ToList();
                });

希望这可以帮助到其他人

大卫