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:
[
{
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();