I’m trying to convert some code that uses datasets to LINQ. Some of the code passes column names into other functions as strings.
Is there anyway I can easily rewrite this into LINQ?
string s = getElement(tr, elementName); private string getElement (tableRow re, string elementName){ if(tr[elementName] != null){ return tr[elementName].toString() } }
OR:
private void copy (tableRow trFrom, tableRow trTo){ foreach (DataColumn c in trFrom.Table.Columns) { trTo[c.ColumnName] = trFrom[c.ColumnName]; } }
Answer to GVS: The reason to convert to LINQ is because it in many situations are easier to code LINQ and get better performance. It’s related to another question here on stackoverflow: programming pattern using typed datasets
The reason I need to use the column name as a string is mainly because the column names are passed as a ID for input fields, they are then sent back to the program using AJAX (jquery).
The answer is to use reflection.
Getting a value
Or the copy scenario: