few dayz ago came across THIS question.
the solution to this can easily be figured out by the new dynamic features introduced in .Net 4.0.
i have made a project to demonstrate this.
the main code in the project goes like this
the solution to this can easily be figured out by the new dynamic features introduced in .Net 4.0.
i have made a project to demonstrate this.
the main code in the project goes like this
public static dynamic[] GetDynamicObject(DataTable dt, Dictionary<string, string> mapping)
{
List<dynamic> returnList = new List<dynamic>();
foreach (DataRow row in dt.Rows)
{
dynamic obj = new ExpandoObject();
var objectDic = (IDictionary<string, object>)obj;
foreach (DataColumn column in dt.Columns)
{
objectDic.Add(mapping!=null?mapping.ContainsKey(column.ColumnName) ? mapping[column.ColumnName] : column.ColumnName:column.ColumnName,row[column]);
}
returnList.Add(obj);
}
return returnList.ToArray();
}
The static method here named "GetDynamicObject" simply takes a data table(which can be populated by the data in the database) and a dictionary of <string,object> as the mapping.
then for each row in the DataTable the method makes a dynamic Object of type ExpandoObject which was introduced for the sole purpose of adding and consuming the propertiese at runtime. more info about expando object can be got from here(MSDN link).
The full project i made is available here(Sky Drive link).
i dont what happend with the formatting.. so had to do this.. itz still readable... nd that solves the purpose
ReplyDeletei liked content of your blog.but i couldn't read ur font well.i used 16px for my blog:http://intechera.blogspot.com
ReplyDelete