You can fill List of object from DataTable using LINQ. There are many situations where you need to get data from DB into data table and after that from data table to proper class object. We can do this easily with LINQ.
Here is example for this.
In this example we take one DataTable object which contains two columns 'product_id' and 'product_name'. This DataTable has many rows. Now we want to convert DataTable object into list of class object. We have one Class 'clsProduct' which has two property 'product_id' and 'product_name'. Now we transform DataTable object data in to list of clsProduct class object using LINQ.
C#. Net Example :
VB.Net Examples :
Output :
Below are the books that you would like :
Here is example for this.
In this example we take one DataTable object which contains two columns 'product_id' and 'product_name'. This DataTable has many rows. Now we want to convert DataTable object into list of class object. We have one Class 'clsProduct' which has two property 'product_id' and 'product_name'. Now we transform DataTable object data in to list of clsProduct class object using LINQ.
C#. Net Example :
private DataTable GetProducts() { DataTable dt = new DataTable(); DataColumn dc; DataRow dr; dt.TableName = "product"; dc = new DataColumn("product_id", int.MaxValue.GetType()); dt.Columns.Add(dc); dc = new DataColumn("product_name"); dt.Columns.Add(dc); dr = dt.NewRow(); dr["product_id"] = 1; dr["product_name"] = "Monitor"; dt.Rows.Add(dr); dr = dt.NewRow(); dr["product_id"] = 2; dr["product_name"] = "Mouse"; dt.Rows.Add(dr); dr = dt.NewRow(); dr["product_id"] = 3; dr["product_name"] = "KeyBoard"; dt.Rows.Add(dr); dr = dt.NewRow(); dr["product_id"] = 4; dr["product_name"] = "LCD"; dt.Rows.Add(dr); return dt; } public class clsProduct { public int product_id; public string product_name; } protected void Page_Load(object sender, EventArgs e) { DataTable dt = GetProducts(); List<clsProduct> lstProduct; lstProduct = ( from m in dt.Rows.Cast<DataRow>() select new clsProduct { product_id = m.Field<Int32>("product_id"), product_name = m.Field<string>("product_name") } ).ToList(); }
VB.Net Examples :
Private Function GetProducts() As DataTable Dim dt As New DataTable() Dim dc As DataColumn Dim dr As DataRow dt.TableName = "product" dc = New DataColumn("product_id", Integer.MaxValue.[GetType]()) dt.Columns.Add(dc) dc = New DataColumn("product_name") dt.Columns.Add(dc) dr = dt.NewRow() dr("product_id") = 1 dr("product_name") = "Monitor" dt.Rows.Add(dr) dr = dt.NewRow() dr("product_id") = 2 dr("product_name") = "Mouse" dt.Rows.Add(dr) dr = dt.NewRow() dr("product_id") = 3 dr("product_name") = "KeyBoard" dt.Rows.Add(dr) dr = dt.NewRow() dr("product_id") = 4 dr("product_name") = "LCD" dt.Rows.Add(dr) Return dt End Function Public Class clsProduct Public product_id As Integer Public product_name As String End Class Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load Dim dt As DataTable = GetProducts() Dim lstProduct As List(Of clsProduct) lstProduct = ( From m In dt.Rows.Cast(Of DataRow)() Select New clsProduct With { _ .product_id = m.Field(Of Int32)("product_id"), _ .product_name = m.Field(Of String)("product_name") _ }).ToList() End Sub
Output :
Below are the books that you would like :
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.
ReplyDeleteSoftware Testing Training in Chennai
This blog gives very important info about .Net Thanks for sharing
ReplyDeleteDot Net Online Training Bangalore
This is ansuperior writing service point that doesn't always sink in within the context of the classroom. In the first superior writing service paragraph you either hook the reader's interest or lose it. Of course your teacher, who's getting paid to teach you how to write an good essay,
ReplyDeletepython training in chennai | python training in bangalore
python online training | python training in pune
python training in chennai | python training in bangalore
You blog post is just completely quality and informative. Many new facts and information which I have not heard about before. Keep sharing more blog posts.
ReplyDeleteData Science Training in Chennai
Data science training in bangalore
Data science online training
Data science training in pune
Data science training in kalyan nagar
selenium training in chennai
Whoa! I’m enjoying the template/theme of this website. It’s simple, yet effective. A lot of times it’s very hard to get that “perfect balance” between superb usability and visual appeal. I must say you’ve done a very good job with this.
ReplyDeleteAWS Online Training | Online AWS Certification Course - Gangboard
Best Selenium Training in Chennai | Selenium Training Institute in Chennai | Besant Technologies
Selenium Training in Bangalore | Best Selenium Training in Bangalore
AWS Training in Bangalore | Amazon Web Services Training in Bangalore
Amazon Web Services Training in Pune | Best AWS Training in Pune
Hi there I am so thrilled I found your website, I really found you by mistake, while I was browsing on Yahoo for something else
ReplyDeleteiosh course in chennai
Thanks admin for spending your valuable time to write an excellent article.
ReplyDeleteselenium Training in Chennai
Selenium Training Chennai
ios training institute in chennai
.Net coaching centre in chennai
French Classes in Chennai
php courses in chennai
PHP Training in Velahery
Awesome article. It is so detailed and well formatted that i enjoyed reading it as well as get some new information too.
ReplyDeletedevops online training
aws online training
data science with python online training
data science online training
rpa online training
Thank you for taking the time to provide us with your valuable information. We strive to provide our candidates with excellent care and we take your comments to heart.As always, we appreciate your confidence and trust in us
ReplyDeleteMicrosoft Azure online training
Selenium online training
Java online training
uipath online training
Python online training
Thanks for one marvelous posting! I enjoyed reading it; you are a great author. I will make sure to bookmark your blog and may come back someday. I want to encourage that you continue your great posts.
ReplyDeletesap hybris training in bangalore
sap scm training in bangalore
sap pm training in bangalore
sap crm training in bangalore
sap ewm training in bangalore
Thank you for your post. This is excellent information. It is amazing and wonderful to visit your site.Prathima Infotech training center bangalore
ReplyDeleteSAP APO Training in Bangalore
sap gts training in bangalore
sap testing training in bangalore
sap fiori training in bangalore
Hardware and Networking Training in Chennai
ReplyDeleteCCNA Training in Chennai
AWS Training in Chennai
SAP Training in Chennai
Software Testing Training in Chennai
Java Training in Chennai
SAP Training in Chennai
Actually I read it yesterday but I had some thoughts about it and today I wanted to read it again because it is very well written.
ReplyDeletedata science course
Great Blog. Thanks.
ReplyDeleteSAP Training in Chennai
Java Training in Chennai
Software Testing Training in Chennai
.Net Training in Chennai
Hardware and Networking Training in Chennai
AWS Training in Chennai
Azure Training in Chennai
Selenium Training in Chennai
QTP Training in Chennai
Android Training in Chennai
feel really happy to have seen your web page and look forward to so many more entertaining times reading here. Thanks once more for all the details.I definitely really liked every part of it and i also have you saved to look at new information in your site.
ReplyDeleteData Science Training In Chennai | Certification | Data Science Courses in Chennai | Data Science Training In Bangalore | Certification | Data Science Courses in Bangalore | Data Science Training In Hyderabad | Certification | Data Science Courses in hyderabad | Data Science Training In Coimbatore | Certification | Data Science Courses in Coimbatore | Data Science Training | Certification | Data Science Online Training Course
I read this post two times, I like it so much, please try to keep posting & Let me introduce other material that may be good for our community.
ReplyDeleteNice post. Thanks for sharing! I want people to know just how good this information is in your article. It’s interesting content and Great work. nice to read.
ReplyDeletehardware and networking training in chennai
hardware and networking training in tambaram
xamarin training in chennai
xamarin training in tambaram
ios training in chennai
ios training in tambaram
iot training in chennai
iot training in tambaram
Well somehow I got to read lots of articles on your blog. It’s amazing how interesting it is for me to visit you very often.
ReplyDeleteweb designing training in chennai
web designing training in omr
digital marketing training in chennai
digital marketing training in omr
rpa training in chennai
rpa training in omr
tally training in chennai
tally training in omr
Glad to chat your blog,I seem to be forward to more reliable articles and i think we all wish to thank so many good articles,blog to share with us.data scientist courses
ReplyDeleteThank you for posting informative insights, I think we have got some more information to share with! Do check out
ReplyDeleteoracle training in chennai and let us know your thoughts. Let’s have great learning!
Thank you For your Valuable Info.
ReplyDeleteSap EHS training in bangalore
This post is very simple to read and appreciate without leaving any details out. Great work!
ReplyDeletedata scientist course in hyderabad
Learn Amazon Web Services for excellent job opportunities from Infycle Technologies, the best AWS training center in Chennai. Infycle Technologies gives the most trustworthy AWS course in Chennai, with full hands-on practical training from professional trainers in the field. Along with that, the placement interviews will be arranged for the candidates, so that, they can meet the job interviews without missing them. To transform your career to the next level, call 7502633633 to Infycle Technologies and grab a free demo to know more
ReplyDeleteTOP AWS TRAINING IN CHENNAI
Your amazing insightful information entails much to me and especially to my peers. Thanks a ton; from all of us.
ReplyDeletedata scientist training and placement
Your article increases the curiosity to learn more about this topic. Keep sharing your information regularly for future reference.
ReplyDeleteDevOps Training in Hyderabad
DevOps Course in Hyderabad
Extremely overall quite fascinating post. I was searching for this sort of data and delighted in perusing this one. Continue posting. A debt of gratitude is in order for sharing. cloud computing course in nagpur
ReplyDeleteAI Patasala is the most rumoured Python Training in Hyderabad developed to help you advance your career with a massive increase in the latest technology.
ReplyDeletePython Training in Hyderabad
I am overwhelmed by your post with such a nice topic. Usually I visit your blogs and get updated through the information you include but today’s blog would be the most appreciable. Well done!
ReplyDeletecloud computing course in hyderabad
Thanks for sharing this information. I really like your blog post very much. You have really shared a informative and interesting blog post with people..
ReplyDeleteethical hacking training in hyderabad
Your blog provided us with valuable information to work with. Each & every tip of your post is awesome. Thanks a lot for sharing. Keep blogging,
ReplyDeletedata science training institute in hyderabad
Such a good post .thanks for sharing
ReplyDeleteIELTS Training in T Nagar
IELTS Training in T Nagar
Informative blog and knowledgeable content. Thanks for sharing this awesome blog with us. If you want to learn data science then follow the below link.
ReplyDeleteData Science Training in Hyderabad
Happy to visit your blog, I am by all accounts forward to more solid articles and I figure we as a whole wish to thank such huge numbers of good articles, blog to impart to us.
ReplyDeletedata science course in hyderabad
I have to thank you for the time i spent on this especially great reading !! i really liked each part and also bookmarked you for new information on your site.
ReplyDeleteGreat information.Thanks for sharing the article
Data science course in hyderabad
Data science training in hyderabad
very well explained.very useful.Thanks for the post.
ReplyDeleteSQL Course in Pune