0

I have a table with many columns and I need to update the column that matches a set of parameters. Is it possible to concatenate a string and then use the string result to update a matching named column of a database using Telerik's OpenAccess? If so, I'm thinking reflection is required here? I would like to be able to do something like shown below:

A simplified example table:

Sku         QtyOnHand   Whse1Aug2017   Whse2Aug2017   Whse3Aug2017
==================================================================
ABC-123     87            2               4             8
XYZ-789     43            0               5             4


string warehouseId = "1"
string month = "Aug"
string year = "2017"
string sku = "ABC-123"
int qtySold = 3;

string columnName = "Whse" + warehouseId + month + year;

var query = (from s in model.Sales
            where s.SKU == sku
            select s).FirstOrDefault();

query.columnName = query.columnName + qtySold;
query.SaveChanges();
PixelPaul
  • 2,609
  • 4
  • 39
  • 70

2 Answers2

2

You can use Reflection for this. Eg

void UpdateProperty(object targetObject, string propertyName, object value)
{
    var pi = targetObject.GetType().GetProperty(propertyName);
    pi.SetValue(targetObject, value);
}
David Browne - Microsoft
  • 80,331
  • 6
  • 39
  • 67
0

The Reflection would do it but is a bit expensive.

Telerik Data Access does this out of the box. Check the API for setting and getting values from artificial types/fields. See the section "Setting and Getting Artificial Properties/Fields": http://docs.telerik.com/data-access/feature-reference/api/context-api/feature-ref-api-context-api-artificial-data-api

here is an example how it works:

category.SetFieldValue( "CategoryName", "MyCategory" );

category.SetFieldValue( "Description", "New Description" );