Create a data table by querying through LINQ

Source: Internet
Author: User

Using system; <br/> using system. collections. generic; <br/> using system. LINQ; <br/> using system. text; <br/> using system. data; </P> <p> namespace usecopytotable <br/> {<br/> class Program <br/>{< br/> static void main (string [] ARGs) <br/>{< br/> usecopytodtsimple (); <br/> usesetfield (); <br/>}</P> <p> static dataset builddataset () <br/>{< br/> // create a students dataset <br/> dataset DS = new dataset ("Students"); <br/> // create a students data table, and add it to the dataset <br/> // students data table contains student information <br/> datatable dtstu = new datatable ("Students"); <br/> DS. tables. add (dtstu); <br/> // Add the column information of the student information record <br/> dtstu. columns. addrange (New datacolumn [] {<br/> New datacolumn ("name", type. getType ("system. string "), <br/> New datacolumn (" xingbie ", type. getType ("system. string "), <br/> New datacolumn (" Age ", type. getType ("system. int32 "), <br/> New datacolumn (" scoreid ", type. getType ("system. int32 "), <br/>}); <br/> // Add the row information of the Student Information <br/> dtstu. rows. add ("Zhang San", "male", 20, 1); <br/> dtstu. rows. add ("Li Si", "male", 19, 2); <br/> dtstu. rows. add ("Wang Xia", "female", 21, 3); <br/> dtstu. rows. add ("Zhao Min", "female", 22, 4); <br/> dtstu. rows. add ("Wu an", "male", 18, 5); <br/> // create a scores data table, and add it to the dataset <br/> // The scores data table contains the Student Score record <br/> datatable dtscore = new datatable ("scores"); <br/> DS. tables. add (dtscore); <br/> // Add the column information of the score record <br/> dtscore. columns. addrange (New datacolumn [] {<br/> New datacolumn ("scoreid", type. getType ("system. int32 "), <br/> New datacolumn (" math ", type. getType ("system. int32 "), <br/> New datacolumn (" Chinese ", type. getType ("system. int32 "), <br/> New datacolumn (" English ", type. getType ("system. int32 "), <br/>}); <br/> // Add a student score record <br/> dtscore. rows. add (1, 80, 75, 78); <br/> dtscore. rows. add (3, 88, 80, 60); <br/> dtscore. rows. add (4, 75, 90, 80); <br/> dtscore. rows. add (5, 59, 80, 75); <br/> // return the dataset <br/> return Ds; <br/>}</P> <p> static void usecopytodtsimple () <br/>{< br/> // obtain the dataset and the data table to be queried <br/> dataset DS = builddataset (); <br/> datatable dtstu = Ds. tables ["Students"]; <br/> datatable dtscore = Ds. tables ["scores"]; <br/> // query the students whose score is greater than 20 in query1 <br/> var query1 = <br/> from Stu in dtstu. asenumerable () <br/> from score in dtscore. asenumerable () <br/> where stu. field <int> ("scoreid") = score. field <int> ("scoreid") <br/> where (INT) STU ["Age"]> 20 <br/> select Stu; <br/> // create a new copy using the copytodatatable () method <br/> datatable newdt = query1.copytodatatable <datarow> (); <br/> // print the copy information <br/> system. console. writeline ("student list:"); <br/> foreach (VAR item in newdt. asenumerable () <br/>{< br/> system. console. writeline ("Name: {0}, Gender: {1}, age: {2}", <br/> item ["name"], item ["xingbie"], item ["Age"]); <br/>}</P> <p> static void usesetfield () <br/>{< br/> // obtain the dataset and the data table to be queried <br/> dataset DS = builddataset (); <br/> datatable dtstu = Ds. tables ["Students"]; <br/> // increase the age of all students by 2 <br/> foreach (VAR row in dtstu. asenumerable () <br/>{< br/> int age = row. field <int> ("Age"); <br/> row. setfield <int> ("Age", age + 2); <br/>}< br/> // print new student information <br/> system. console. writeline ("student list:"); <br/> foreach (VAR item in dtstu. asenumerable () <br/>{< br/> system. console. writeline ("Name: {0}, Gender: {1}, age: {2}", <br/> item ["name"], item ["xingbie"], item ["Age"]); <br/>}< br/>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.