Pete Pete - 2 years ago 104
C# Question

Map 2 arrays to list of objects with each array as a property

In kentico categories for pages are held in an object called

IInfoObjectCollection
. Weirdly, it holds the category details in separate arrays so the category ids are held in an array of Guids and then the category names are held in an array of strings in this object collection

Example of the type of class I have in the kentico treenode:

public class InfoObjectCollection
{
public Guid[] CategoryIDs { get; set; }
public string[] Names { get; set; }
}


Now I have a category class:

public class Category
{
public Guid CategoryID { get; set; }
public string Name { get; set; }
}


Is there a way to use automapper and linq (or not) to map the InfoObject into a collection of categories?

Answer Source

AM seems overkill

source.CategoryIDs.Zip(source.Names, (id, name)=>new Category { CategoryID = id, Name = name});
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download