C# find a value from an object column using a string from another column

Posted by Graham on Programmers See other posts from Programmers or by Graham
Published on 2012-03-22T11:35:40Z Indexed on 2012/03/22 11:38 UTC
Read the original article Hit count: 213

Filed under:

I have 2 list in foreach loops.. I currently use a 'switch' statement on the m.columnname and then assign the value of that column to another var map as per below..

If m.columnname = 'DocHeading' then v.docheading is assigned to map.value..

There will always be a match on m.columnname to a column in the var v.

is there a way to get the value from the var v using the string from m.columnname? The reason is that the users will want to add and change column names and I dont want to have to change this code all the time..

Hope this makes sense

List spreadMapping = new List(); foreach (var m in mappings) { foreach (var v in hvalues) { SpreadMappings map = new SpreadMappings(); switch (m.ColumnName) { case “DocHeading”: map.ColumnX = m.ColumnX; map.ColumnY = m.ColumnY; map.ColumnValue = v.DocHeading; map.ColumnName = m.ColumnName; map.ColumnId = v.Id; map.ColumnSheetName = sheetName; spreadMapping.Add(map); break;

© Programmers or respective owner

Related posts about c#