English 中文(简体)
4. 自动采集探测仪,在收集物体时标注特性
原标题:AutoMapper collections of strings to object properties in a collection of objects

I m trying to map an object containing enumerable collections of strings into an object containing an enumerable collection objects whose properties contain the data in those collections.

Consider the following source class:

class Source
{
    public IEnumerable<string> Makes { get; set; }
    public IEnumerable<string> Models { get; set; }
    public IEnumerable<string> Variants { get; set; }
}

并且:

class Destination
{
    public IEnumerable<Vehicle> Vehicles { get; set; }
}

class Vehicle
{
    public string Makes { get; set; }
    public string Models { get; set; }
    public string Variants { get; set; }
}

我愿利用每个<代码>Source 的定位地点,将新的<代码>Vehicle的收集值绘制成<代码>,并在<代码>中添加这一数值。 车辆收集。 因此,如果<代码>Source的藏书各包含20项,我将期望Destination。 载有20个<密码>的车辆收集

我尝试了以下组合,但并没有解决外围问题:

    CreateMap<Source, Destination>();
    CreateMap<Source, Vehicle>();

随后,我继续尝试在,但很快走出我的深度。

任何帮助都会受到高度赞赏。

增 编

问题回答

评论中提到的<< href=”https://docs.automapper.org/en/stable/Custom-type-converters.html

CreateMap<Source, Destination>()
    .ConvertUsing((src, dst) =>
    {
        dst = new Destination();
                    
        dst.Vehicles = src.Makes
            .Zip(src.Models, src.Variants)
            .Select(x => new Vehicle
            {
                Makes = x.Item1,
                Models = x.Item2,
                Variants = x.Item3
            })
            .ToList();
                    
            return dst;
});

If your .NET Version doesn t support the Enumerable.Zip() with three parameters, then you have to implement to merge the arrays as below:

int maxElement = new int[] { src.Makes.Count(), src.Models.Count(), src.Variants.Count() }
    .Max();
                    
var vehicles = new List<Vehicle>();
for (int i = 0; i < maxElement; i++)
{
    vehicles.Add(new Vehicle
        {
            Makes = src.Makes.ElementAtOrDefault(i),
            Models = src.Models.ElementAtOrDefault(i),
            Variants = src.Variants.ElementAtOrDefault(i)
        });
}
dst.Vehicles = vehicles;




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...