I need a function where if I pass class name or object of class with dataTable that function should return List
Ex :
Public List
{
//
Return List
}
How to create above function ?
I need a function where if I pass class name or object of class with dataTable that function should return List
Ex :
Public List
{
//
Return List
}
How to create above function ?
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Rahul SinghRank #1,323 · 1.3K reputationPosted Nov 24, 2014, 12:29 AM EST
I have created this generic method to convert a datatable to generic list. Hope this the solution you were looking for.
private static List ConvertDataTable(DataTable dt) data = new List();(row);
{
List
foreach (DataRow row in dt.Rows)
{
T item = GetItem
data.Add(item);
}
return data;
}
private static T GetItem(DataRow dr)();
{
Type temp = typeof(T);
T obj = Activator.CreateInstance
foreach (DataColumn column in dr.Table.Columns)
{
foreach (PropertyInfo pro in temp.GetProperties())
{
if (pro.Name == column.ColumnName)
pro.SetValue(obj, dr[column.ColumnName], null);
else
continue;
}
}
return obj;
}
I have attached the sample code too, do let me know if that helped.
laxman lucky12 reputationPosted Nov 23, 2014, 11:42 PM EST
convert DataTable to List<> dynamically
When your passing the value you can use this pass paramiter or object
Do somthing like this..
string connectionString = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString.ToString();().Select(r => new Selecting_Entity (List Name)
using (SqlConnection con = new SqlConnection(connectionString))
{
SqlCommand cmd = new SqlCommand("Your seleting procedure name", con);
cmd.CommandType = CommandType.StoredProcedure;
con.Open();
var Reader = cmd.ExecuteReader();
var Result = Reader.Cast
{
Firstname= (string)r["FIRSTNAME"],
Lastname = (string)r["LASTNAME"],
Phonenumber= (int)r["PHONENUM"],
}).ToList();
return Result;
}
Do like this I hope it will help you.
Thanks,
Laxman M,
Abhishek KumarRank #510 · 4.8K reputationPosted Nov 24, 2014, 12:38 AM EST
Please follow the below article .
http://www.codeproject.com/Tips/784090/Conversion-Between-DataTable-and-List-in-Csharp
Hope this help you.
Abhishek