Add, delete, modify, query, and add, delete, and modify

Source: Internet
Author: User

Add, delete, modify, query, and add, delete, and modify
Preface:

LanguageIntegrated Query is a set of extensions for c # and Visual Basic languages. It allows you to write C # Or Visual Basic code to query memory data in the same way as a database.

Technically speaking, LINQ defines about 40 query operators, such as select, from, in, where, and order by (C ). You can use these operators to write query statements. However, these queries can also be based on many types of data. Each data type requires a separate LINQ type.







At the beginning, I always had a feeling that I was very confused when I started to learn about LinQ. Now, I had to learn new content temporarily: PD, EF, LINQ, and MongDB. Isn't everything I learned useful? Ask the masters with such questions. Add the lecture that Mr. Mi gave today. To understand what learning is. At ordinary times, learning is to cultivate a kind of ability. When it comes to real projects, you can easily accept what you want to learn. Well, it's very easy for me, it is acceptable to study four things in five days.

Taking LINQ as an example, if beginners are about to get started with projects, the purpose of learning is very clear. Since it can be used to replace SQL statements, it is clear that it is the first thing to do first to understand its addition, deletion, modification, and query. Technically speaking, LINQ defines about 40 query operators, such as select, from, in, where, and order by (C ). You can use these operators to write query statements. However, these queries can also be based on many types of data. Each data type requires a separate LINQ type. In fact, look at these high query operators, which are the same as the SQL statements we wrote. It's just a change in order.



Add, delete, modify, and delete a table using the following code:


<Span style = "font-size: 18px;"> using System; using System. collections. generic; using System. linq; using System. web; using System. web. UI; using System. web. UI. webControls; using System. data. common; namespace FirstLinq {public partial class _ Default: Page {NorthwindDataContext ctx = new NorthwindDataContext ("Data Source = .; initial Catalog = linq; Persist Security Info = True; User ID = sa; Password = 123456 "); protecte D void Page_Load (object sender, EventArgs e) {// Linq1 (); // Linq2 (); Linq3 ();} /// <summary> /// add, delete, modify, and query /// </summary> private void Linq3 () {// Add t_User user = new t_User (); user. ID = 1; user. userName = "Zhou"; user. pwd = "11111"; ctx. t_User.InsertOnSubmit (user); ctx. submitChanges (); // change t_User userUpdate = ctx. t_User.SingleOrDefault (t_User => t_User.ID = 0); userUpdate. userName = "Zhou rename"; ctx. submi TChanges (); // Delete t_User userDelete = (from userinfo in ctx. t_User where userinfo. ID = 1 select userinfo). FirstOrDefault (); if (userDelete! = Null) {ctx. t_User.DeleteOnSubmit (userDelete); ctx. SubmitChanges () ;}</span>



LING query:

/// <Summary> /// familiar with the syntax of Linq to SQL /// </summary> private void Linq2 () {Customers customer = new Customers (); // execute common SQL statements to query the record IEnumerable <Customers> MERs = ctx. executeQuery <Customers> ("select * from Customers where CustomerID = 'anact'"); customer = customers. first (); Response. write (customer. customerID); // use Linq to query a single record var cus = from c in ctx. customers where c. customerID. equ Als ("anasd") select c; customer = cus. first (); Response. write (customer. companyName); // query result set. Syntax: from temporary table name in table set orderby temporary table name. field name upgrade sequence select temporary table name gdvCustomers. dataSource = from cust in ctx. customers where cust. customerID! = "Anasd" orderby cust. CompanyName descending select cust; gdvCustomers. DataBind ();}


Summary:


The principle of LINQ is actually quite simple. For example, LINQ combines object-oriented and relational data models. Such a Technology Maps data into an object for convenient operation and management. Instead of generating SQL statements by concatenating strings based on different situations, you can focus on the processing of object models, these operations at the method level are converted into corresponding addition, deletion, modification, and query statements, and all the operations are transferred to the database during the submit operation. All in all, linq to SQL is an intermediate layer from a database to an object structure. It changes the management of relational data into an object operation, blocking the trouble of SQL, we can also get help from vs's powerful smart sensing functions.

 




Related Article

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.