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.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Rahul SinghPosted Nov 24, 2014, 5:29 AM
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.
Abhishek KumarPosted Nov 24, 2014, 5:38 AM
Please follow the below article .
http://www.codeproject.com/Tips/784090/Conversion-Between-DataTable-and-List-in-Csharp
Hope this help you.
Abhishek
laxman luckyPosted Nov 24, 2014, 4:42 AM
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,