LINQ to dataset (III)

Source: Internet
Author: User
ArticleDirectory
    • Single Table query
    • Connection Query
    • LINQ to SQL
Single Table query
 
Datatable dt = Ds. tables ["product"]; var query = // call the asenumerable method to enable the LINQ able object to support LINQ query from R in DT. asenumerable () // use the field method in datarow. The field is generic and the ACCESS format is :. field <type> ("field name") Where R. field ("CID") = 2 select new {id = R. field ("ID"), name = R. field ("name"), cid = R. field ("CID")}; foreach (var p in query) {string MSG = string. format ("ID: {0}, name; {1}, CID: {2}", p. ID, P. name, P. CID); listbox1.items. add (MSG );}
Connection Query
Datatable dt_p = Ds. tables ["product"]; datatable dt_c = Ds. tables ["classify"]; var query = from C in dt_c.asenumerable () join P in dt_p.asenumerable () on C. field ("ID") equals P. field ("CID") Select New {id = P. field ("ID"), name = P. field ("name"), cid = P. field ("CID"), cname = C. field ("name")}; foreach (var p in query) {string MSG = string. format ("ID: {0}, name; {1}, CID: {2}, cname: {3}", p. ID, P. name, P. CID, P. cname); listbox1.items. add (MSG );}

Groupjoin

LINQ to SQL

First, create a LINQ to SQL class.

// LINQ to SQL class: including the classification table classify and product table productlq1datacontext LQ1 = new lq1datacontext (); // obtain all the classification var query = from C in lq1.classify select C; foreach (var c in query) {// note c. product is the product set foreach (var p in C. product) {// note p. classify. name is the product category name string MSG = string. format ("ID: {0}, name; {1}, category: {2},", p. ID, P. name, P. classify. name); listbox1.items. add (MSG );}}

Delete data

 
Lqdatacontext LQ1 = new lqdatacontext (); // method 1 // delete a product with the ID of 100 // delete_product is the Stored Procedure lq1.delete _ product (100) attached to lqdatacontext ); // method 2 // first find the record product PDT = (from P in product where p. id == 100 select P ). first (); // Delete this record lq1.product from the client cache. deleteonsubmit (PDT); // execute the command to delete lq1.submitchanges ();

Modify data

Lqdatacontext LQ1 = new lqdatacontext (); // Method 1: Stored Procedure // The parameters are product ID, name, price, quantity, and category idlq1.update _ product (100, "Nike", 99, 33, 2); // method 2 // first find this record product PDT = (from P in product where p. id == 100 select P ). first (); // modify this record PDT. name = "Nike"; PDT. price = 99; PDT. number = 33; PDT. cid = 2; // Delete lq1.submitchanges ();

Add data

 
Lqdatacontext LQ1 = new lqdatacontext (); // Method 1: Stored Procedure // method 2 Product PDT = new product (); PDT. id = 100pdt. name = "Nike"; PDT. price = 99; PDT. number = 33; PDT. cid = 2; lq1.product. insertonsubmit (p); lq1.submitchanges ();

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.