none
DataTable to List with out Loop RRS feed

  • Question

  • Hi Team,<o:p></o:p>

    I have service oriented architecture i.e. asp.net web UI with web service layer. My requirement is service should return a collection list only not data table. I
    am executing procedure and getting the Data table that data table should be converted as List of custom class then I want to return to my UI.<o:p></o:p>

    I know this can be done by using a loop.

    But I am expecting in some good way to achieve this, can someone please help me?<o:p></o:p>

    Thanks

    Selva.R


    Selvakumar Rathinam Team Lead

    Wednesday, May 8, 2013 6:55 AM

Answers

All replies

  • "But I am expecting in some good way to achieve this,"

    What's not good about using a loop? In general, transforming some kind of collection to another kind of collection requires you to loop through the original collection.

    Wednesday, May 8, 2013 6:59 AM
    Moderator
  • Hi Mike Danes,

                            Ok I understand. Can you please explain me how to do this using Loop?

    Thanks

    Selva.R


    Selvakumar Rathinam Team Lead

    Wednesday, May 8, 2013 8:53 AM
  • First you need to create a class that can store whatever data you have in that table's rows, you cannot put table rows in the list. For example if your DataTable has 2 columns, ID and Name, then something as simple as the following class should do:

    class User {
        public int ID { get; set; }
        public string Name { get; set; }
    }

    Once you have that you go through all the table rows, create User instances and add them to a List<User>:

    List<User> users = new List<User>();
    
    foreach (DataRow row in table.Rows) {
        users.Add(new User {
            ID = (int)row["ID"],
            Name = (string)row["Name"]
        });
    }
    

    Wednesday, May 8, 2013 9:21 AM
    Moderator
  • Ok  Thanks.


    Selvakumar Rathinam Team Lead

    Wednesday, May 8, 2013 9:35 AM
  • Hi,
    Your can create your clas & use LINQ. Like this,

    public class myObject
    {
    	public string Id { get; set; }
    	public string Name { get; set; }
    }


    at your method, use LINQ,

    DataTable dt = new DataTable("test");
    dt.Columns.Add("ID", typeof(int));
    dt.Columns.Add("NAME", typeof(string));
    
    dt.Rows.Add(1, "JON");
    dt.Rows.Add(2, "Rainmaker");
    List<myObject> list = (from row in dt.AsEnumerable()
    					   select new myObject
    					   {
    						   Id = row.Field<int>("ID"),
    						   Name = row.Field<string>("NAME")
    					   }).ToList();


    以上說明若有錯誤請指教,謝謝。
    亂馬客blog: http://www.dotblogs.com.tw/rainmaker/

    Wednesday, May 8, 2013 10:18 AM
  • use DTOs (Data Transport Object), if you can use Entity Framework instead of DataTable then go for it. because It will give you DTOs. and expose DTOs at service layer as Data Contract. so at client side you'll get same same contract.

    Faisal Ahmed Farooqui —————————— If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    Wednesday, May 8, 2013 11:24 AM