dimanche 2 août 2015

Change multiple field values of an object

I have a collection of objects (say Products) and I want to change the some field values of each object in the collection. I want to define the field name and its corresponding value as below

var mapData = new Dictionary<string,string>();
mapData.Add("Name","N");
mapData.Add("Category", "C");

For every pre populated Product object's Name and Category fields values needs to be overwritten with N and C. I was trying to do that using LINQ as below and got stuck.

    [StepArgumentTransformation]
    public IEnumerable<Product> TransformProductData(Table table)
    {
        var mapData = new Dictionary<string,string>();
        mapData.Add("Name","N");
        mapData.Add("Category", "C");

        foreach(var product in table.CreateSet<Product>)
        {
          var transformedProduct = typeof(product).GetProperties().Select
                    (
                        prop => mapData.First(x => x.Key.Equals(prop.Name))
                        // How do I assign the change the values here ??
                    )
        }

    }

Assume the Product Object looks like below

    public class Product
{
    public string Code { get; set; }
    public string Name { get; set; }
    public string Category { get; set; }
    public string Amount { get; set; }
}





Aucun commentaire:

Enregistrer un commentaire