1

I am experimenting to take datatable contents into a list. I am using following code but its not working correctly.

public List<object> ShowMessage() { List<object> obj = new List<object>(); DataTable dt = new DataTable(); dt.Columns.Add("ID"); dt.Columns.Add("Name"); dt.Rows.Add("1","AAA"); dt.Rows.Add("2", "BBB"); dt.Rows.Add("3", "CCC"); foreach (DataRow dr in dt.Rows) { obj.Add(dr); } return obj; } 

I am new and not sure I am doing in a right way or I need to use some thing else. Any suggestion will be highly appreciated.

Thanks.

3
  • 2
    What you are expecting to have in that list? Should it be list of strings "AAA", "BBB", "CCC" or something else? Commented Jan 10, 2014 at 13:37
  • @SergeyBerezovskiy yes, I am expecting a list of strings with "AAA", "BBB" and "CCC". Commented Jan 10, 2014 at 13:41
  • 1
    Side note - ShowMessage is not the best name for method which returns list of some data and doesn't show anything Commented Jan 10, 2014 at 13:41

3 Answers 3

2

Converting your DataTable to list of name strings (with help of Linq to DataSet):

List<string> names = dt.AsEnumerable().Select(r => r.Field<string>("Name")).ToList(); 

Which is same as

List<string> names = new List<string>(); foreach(DataRow r in dt.Rows) names.Add((string)r["Name"]); 
Sign up to request clarification or add additional context in comments.

Comments

1

I think you are making a too abstract example. This one use a possible class named Person

public class Person { public int PersonID; public string Name; // other fields will follow in future } public List<Person> GetPersonList() { List<Person> people = new List<Person>(); // This is just as example, because in real code // you get this table from a database DataTable dt = new DataTable(); dt.Columns.Add("ID"); dt.Columns.Add("Name"); dt.Rows.Add(1,"John"); dt.Rows.Add(2, "Mark"); dt.Rows.Add(3, "Steve"); // Loop over the rows and construct a Person instance for every row // Add that row to the List<Person> to return foreach (DataRow dr in dt.Rows) { Person p = new Person() {PersonID =Convert.ToInt32(dr[0]), Name = dr[1].ToString()); people.Add(p); } return people; } 

By the way, this pattern of code, is exactly what a good ORM do for you. A little research for Entity Framework or Dapper would be very useful

Comments

0

@Karni: you were close to what you need. however below is the modified version of your code example so that you achieve what you need..

public class Obj { public int ID { get; set; } public string Name { get; set; } } public class ListObj : List<Obj> { } class Program { static void Main(string[] args) { DataTable dt = new DataTable(); dt.Columns.Add("ID"); dt.Columns.Add("Name"); dt.Rows.Add("1", "AAA"); dt.Rows.Add("2", "BBB"); dt.Rows.Add("3", "CCC"); ListObj objListObj = new ListObj(); //to fill the list / collection for (int i = 0; i < dt.Rows.Count; i++) { objListObj.Add(new Obj() { ID = Convert.ToInt16(dt.Rows[i][0]), Name = dt.Rows[i][1].ToString() }); } //To verify if the collection is filled. foreach (var item in objListObj) { Console.WriteLine(item.ID + " : " + item.Name); } Console.Read(); } } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.