Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to map an object consisting of arrays on a list?

I have the following classes:

public class EquipmentListFilter
{
    public string ColumnName { get; set; }
    public string Keyword { get; set; }
    public string Operand { get; set; }
    public string FilterType { get; set; }
}

public class EquipmentFilterPack
{
    public string[] ColumnNames { get; set; }
    public string[] keywords { get; set; }
    public string[] FilterTypes { get; set; }
    public string[] Operands { get; set; }
}

I get the EquipmentFilterPack object as input in my API. I have to map EquipmentFilterPack to a list of EquipmentListFilter. The final result should be List<EquipmentListFilter>.

For example:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

[
    {
        ColumnName = "C1",
        Keyword = "kw1",
        Operand = "AND",
        FilterType = "ٍIS"
    },
    {
        ColumnName = "C2",
        Keyword = "kw2",
        Operand = "AND",
        FilterType = "ٍIS NOT"
    },
    .
    .
    .
]

A similar thread is here.

How can I create the final list? Is there any Linq solution?

>Solution :

As clarified that those four properties arrays have the exact same length, the easiest way is you can use a for loop to iterate and get the element by an index that has been covered in the comment.

Another approach will be performing CROSS JOIN via LINQ:

Query expression

List<EquipmentListFilter> filters = (from a in pack.ColumnNames
                   from b in pack.keywords
                   from c in pack.FilterTypes
                   from d in pack.Operands
                   select new EquipmentListFilter
                    {
                        ColumnName = a,
                        Keyword = b,
                        FilterType = c,
                        Operand = d,
                    }).ToList();

Demo @ .NET Fiddle

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading