Easy-to-use LINQ (015): operations that can be performed using LINQ to SQL

Source: Internet
Author: User

Http://u.115.com/file/f2f877c8d1 demo

LINQ to SQL supports all the key functions that SQL developers expect. You can query information in a table, insert information in the table, and update and delete information in the table.

Select
You can select (Project) by writing a LINQ query in your own programming language and then executing the query to retrieve the result ). To convert all necessary operations into the necessary SQL operations that you are familiar. For more information, see LINQ to SQL.
In the following example, retrieve the company name of a customer from London and display it in the console window.

NorthwindDataContext db = new NorthwindDataContext();   var CompanyNames = from Customer in db.Customers                      where Customer.City == "London"                     select Customer.CompanyName;     foreach (var Customer in CompanyNames)   {       Console.WriteLine(Customer);   }  NorthwindDataContext db = new NorthwindDataContext();var CompanyNames = from Customer in db.Customers                   where Customer.City == "London"                   select Customer.CompanyName;foreach (var Customer in CompanyNames){    Console.WriteLine(Customer);} 

Insert
To execute SQL insert, you only need to add an object to the object model you have created, and then call submitchanges for datacontext.
In the following example, a new customer and information about the customer are added to the MERs table by using insertonsubmit.

NorthwindDataContext db = new NorthwindDataContext();     Customers NewCustomer = new Customers();   NewCustomer.CompanyName = "SomeCompany";   NewCustomer.City = "London";   NewCustomer.CustomerID = "98128";   NewCustomer.PostalCode = "55555";   NewCustomer.Phone = "555-555-5555";   db.Customers.InsertOnSubmit(NewCustomer);     db.SubmitChanges();  NorthwindDataContext db = new NorthwindDataContext();Customers NewCustomer = new Customers();NewCustomer.CompanyName = "SomeCompany";NewCustomer.City = "London";NewCustomer.CustomerID = "98128";NewCustomer.PostalCode = "55555";NewCustomer.Phone = "555-555-5555";db.Customers.InsertOnSubmit(NewCustomer);db.SubmitChanges(); 

Update
To update a database item, you must first retrieve the item and edit it directly in the object model. After modifying this object, call submitchanges for datacontext to update the database.
In the following example, retrieve all customers from London. Change the name of the city from "London" to "London-Metro ". Finally, call submitchanges to send the changes to the database.

NorthwindDataContext db = new NorthwindDataContext();     var LondonCustomers = from Customer in db.Customers                         where Customer.City.Contains("London")                         select Customer;     foreach (var Customer in LondonCustomers)   {       if (Customer.City == "London")       {           Customer.City = "London - Metro";       }   }     db.SubmitChanges();  NorthwindDataContext db = new NorthwindDataContext();var LondonCustomers = from Customer in db.Customers                      where Customer.City.Contains("London")                      select Customer;foreach (var Customer in LondonCustomers){    if (Customer.City == "London")    {        Customer.City = "London - Metro";    }}db.SubmitChanges();

Delete
To delete an item, remove it from its collection, and call submitchanges for datacontext to submit the changes.
Description
The cascading deletion operation cannot be identified by the LINQ to SQL statement. If you want to delete rows from a table with row constraints, see How to: delete rows from a database (LINQ to SQL ).

In the following example, the customer whose customerid is 98128 is retrieved from the database. Then, call deleteonsubmit after you confirm to retrieve the customer line to remove the object from the set. Finally, submitchanges is called to forward the deleted content to the database.

NorthwindDataContext db = new NorthwindDataContext();     var DeletedCustomers = from Customer in db.Customers                         where Customer.CustomerID == "98128"                        select Customer;     if (DeletedCustomers.Count() > 0)   {       db.Customers.DeleteOnSubmit(DeletedCustomers.First());       db.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.