Lock
Lock

Reputation: 5522

How do I make my method generic whereby I can pass in properties?

I have the below code that I am wanting to turn into a generic method.

    List<VariationAttribute> variationAttributeList = new List<VariationAttribute>();

    //get a distinct list of colours
    var colours = ap21Products.GroupBy(p => p.ColourCode).Select(g => g.First()); --this is different

    var colourVariationAttribute = new VariationAttribute();
    colourVariationAttribute.AttributeId = "color"; --this is different
    colourVariationAttribute.VariationAttributeId = "color"; --this is different
    colourVariationAttribute.DisplayName = new DisplayName() { Text = "Colour" }; --this is different
    colourVariationAttribute.VariationAttributeValues = new VariationAttributeValues();

    foreach (var c in colours)
    {
        var attributeValue = new VariationAttributeValue();
        attributeValue.Value = c.ColourCode; --this is different
        attributeValue.DisplayValue = new DisplayValue() { Text = c.ColourName }; --this is different

        colourVariationAttribute.VariationAttributeValues.VariationAttributeValue.Add(attributeValue);
    }
    variationAttributeList.Add(colourVariationAttribute);

I've commented where I want to pass these values in- the string values are fine but I am not sure how I go about passing in a dynamic property name.

For example, on the 4th line, It is grouping by p.ColourCode, however I want to be able to pass in the property to group on.

Likewise, 4 lines from the bottom, the Text property is being set to c.ColourName but I want to pass in that property to show there too.

How would I go about this? I've tried using a signature like this but haven't been successful:

private VariationAttribute createVariationAttribute<TKey>(
    IList<Ap21Product> ap21Products, 
    string attributeId,
    string attributeName,
    Func<Ap21Product, TKey> groupingProperty,
    Func<Ap21Product, TKey> groupingPropertyValue,
    )

Upvotes: 0

Views: 85

Answers (1)

Rob
Rob

Reputation: 27357

You can do something like this:

void something(IList<Ap21Product> ap21Products,
    string attributeId,
    string attributeName,
    Func<Ap21Product, object> grouper,
    Func<Ap21Product, string> colourCode,
    Func<Ap21Product, string> displayValueText 
    )
{
    List<VariationAttribute> variationAttributeList = new List<VariationAttribute>();

    //get a distinct list of colours
    var colours = ap21Products.GroupBy(grouper).Select(g => g.First()); // this is different

    var colourVariationAttribute = new VariationAttribute();
    colourVariationAttribute.AttributeId = "color"; // this is different
    colourVariationAttribute.VariationAttributeId = "color"; // this is different
    colourVariationAttribute.DisplayName = new DisplayName() { Text = "Colour" }; // this is different
    colourVariationAttribute.VariationAttributeValues = new VariationAttributeValues();

    foreach (var c in colours)
    {
        var attributeValue = new VariationAttributeValue();
        attributeValue.Value = colourCode(c);
        attributeValue.DisplayValue = new DisplayValue() { Text = displayValueText(c) };

        colourVariationAttribute.VariationAttributeValues.VariationAttributeValue.Add(attributeValue);
    }
    variationAttributeList.Add(colourVariationAttribute);
}

Upvotes: 1

Related Questions