Social Icons

Tuesday, February 14, 2012

Converting Your DataTable into List<T>Using Linq Query

This example will help you converting your DataTable into List<T>, this topic is specially helpful for development in silverlight, as DataTables doesnot exist in it, so we need to have to convert DataTable into List<T> or ObservableCollection<T>.

Suppose following is your DataTable
DataTable dtExample = new DataTable("Example");
dtExample.Columns.Add("RollNo");
dtExample.Columns.Add("Name");
dtExample.Columns.Add("Class");


The process of converting above DataTable completes in three steps.
Step1: Make a class having properties with respect to your DataTable Columns, In the Example Table there are three Columns
>> RollNo
>> Name
>> class
So You have to Make the following Class
public class Employees
{
        public String RollNo { get ;  set ;}
        public String Name{ get ;  set ;}
        public String Class{ get ;  set ;}
}

Step2: Make a Function of return type of your above class, that will convert a DataTable Row in List<T> Row, This function will be called in Linq Query in Third Step
private Employees GetEmpDataTableRow(DataRow dr)
{
     Employees oEmp = new Employees();
     oEmp.RollNo = dr["RollNo"].ToString();
     oEmp.Name = dr["Name"].ToString();
     oEmp.Class = dr["Class"].ToString();
     return oEmp ;
}
Step3: In the last you are able to convert your DataTable dtExample into List<Employees&gt. go through the Third Step
List<Employees> LstEmployees = new List<Employees>();
LstEmployees = new List<Employees>(
               (from dRow in dtExample.AsEnumerable()
                select (GetEmpDataTableRow(dRow)))
               );
  

Now LstEmployees is your List of Type List<Employees>, which is converted from DataTable dtExample Hope this will help you and work for you, You can contact in case of any confusion. Thanks for my colleague Mr. Jamal Khan who helped in the above solution

10 comments:

  1. a very nice article...thanks

    ReplyDelete
    Replies
    1. Hi, Great.. Tutorial is just awesome..It is really helpful for a newbie like me.. I am a regular follower of your blog. Really very informative post you shared here. Kindly keep blogging. If anyone wants to become a .Net developer learn from Dot Net Training in Chennai. or learn thru ASP.NET Essential Training Online . Nowadays Dot Net has tons of job opportunities on various vertical industry.
      or Javascript Training in Chennai. Nowadays JavaScript has tons of job opportunities on various vertical industry.

      Delete
  2. Man! Thanks A lot, this example was quite useful!

    ReplyDelete
  3. It is amazing and wonderful to visit your site.Thanks for sharing this information,this is useful to me...
    Best Devops online Training
    Online DevOps Certification Course - Gangboard
    Best Devops Training institute in Chennai

    ReplyDelete
  4. I appreciate that you produced this wonderful article to help us get more knowledge about this topic.
    I know, it is not an easy task to write such a big article in one day, I've tried that and I've failed. But, here you are, trying the big task and finishing it off and getting good comments and ratings. That is one hell of a job done!



    Selenium training in bangalore
    Selenium training in Chennai
    Selenium training in Bangalore
    Selenium training in Pune
    Selenium Online training

    ReplyDelete
  5. I appreciate that you produced this wonderful article to help us get more knowledge about this topic.
    I know, it is not an easy task to write such a big article in one day, I've tried that and I've failed. But, here you are, trying the big task and finishing it off and getting good comments and ratings. That is one hell of a job done!



    Selenium training in bangalore
    Selenium training in Chennai
    Selenium training in Bangalore
    Selenium training in Pune
    Selenium Online training

    ReplyDelete
  6. Very nice post here and thanks for it .I always like and such a super contents of these post.Excellent and very cool idea and great content of different kinds of the valuable information's.
    Python Online certification training
    python Training institute in Chennai
    Python training institute in Bangalore

    ReplyDelete
  7. Thanks for posting useful information.You have provided an nice article, Thank you very much for this one. And i hope this will be useful for many people.. and i am waiting for your next post keep on updating these kinds of knowledgeable things...Really it was an awesome article...very interesting to read..please sharing like this information......
    honor service center in chennai
    honor service centre chennai
    honor service centre
    honor mobile service center in chennai
    honor mobile service center

    ReplyDelete

 

Sample text

Sample Text