I have an iterative C# loop which fills out a checkboard pattern of up to 5 columns.
The values are paired, it's always a Headline and multiple Values for each column, and it's combining the values to a non-repetitative combination.
Starting with the simplest solution I could imagine, and after looking at it, I thought there must be a better approach to this problem by doing this recursively.
Below is an example of what I've tried so far:
List<EtOfferVariant> variants = new List<EtOfferVariant>();
_containers[0].Variant.ForEach(first =>
{
if (_containers.Count > 1)
{
_containers[1].Variant.ForEach(second =>
{
if (_containers.Count > 2)
{
_containers[2].Variant.ForEach(third =>
{
EtOfferVariant va = new EtOfferVariant();
va.OfferVariant1Type = _containers[0].VariantKey;
va.OfferVariant1 = first;
va.OfferVariant2Type = _containers[1].VariantKey;
va.OfferVariant2 = second;
va.OfferVariant3Type = third;
va.OfferVariant3 = _containers[3].VariantKey;
variants.Add(va);
});
}
else
{
EtOfferVariant va = new EtOfferVariant();
va.OfferVariant1Type = _containers[0].VariantKey;
va.OfferVariant1 = first;
va.OfferVariant2Type = second;
va.OfferVariant2 = _containers[1].VariantKey;
variants.Add(va);
}
});
}
else
{
EtOfferVariant va = new EtOfferVariant();
va.OfferVariant1Type = _containers[0].VariantKey;
va.OfferVariant1 = first;
variants.Add(va);
}
});
The containers consist of a list of strings (the values) and a key (the headline).
It's a shortend version OfferVariant
counts up to 5 in the real example.
I cant change the inital checkboard structure since its given by a existing database.
Below is an illustration of the data input and output for 2 containers consisting of:
Container 1:
- Key: Pie
- Values:
- Raspberry
- Strawberry
Container 2:
- Key: Drink
- Values:
- Cola,
- Coffee
The generated output would consist of 4 rows containing
edit due the fact its easyly missunderstood as its illustrated here
The Result will be a Row in a Database consisting of 4 columns
Column 1 | Column 2 | Column 3 | Column 4
Pie | Raspberry | Drink | Cola
Pie | Raspberry | Drink | Coffee
Pie | Strawberry| Drink | Cola
Pie | Strawberry| Drink | Coffee
EtOfferVariant is a ORM Poco containing those columns