"2017-06-01" LINQ base +LAMBDA expression implements additions and deletions to the database

Source: Internet
Author: User

One, LINQ to SQL class

High-integrated database access technology

LINQ can be used instead of the previous ADO. Eliminates the massive work of the entity classes and data access classes that knock the code yourself.

Entity class:

Add a LINQ to SQL class----Select the database to use----drag into the table you want to use as if the entity class was finished.

If a property extension is made, the class is added under App_Code, and the class name matches the table name. Add the class to the access modifier with partial to make the class part of the entity class

Data Access classes:

No more data access classes are built under App_Code.

Write directly in the database context where the data access class is used.

Conn.       Users.tolist (); is to convert the users table to a generic collection. It is equivalent to querying the entire method.

Add Data:

Each piece of data is an entity class object. To instantiate it first.

Assigns a value to each property of the object.

Adding data operations using the database context

Added method: Con. Users.insertonsumbit (object);

Perform the method to be manipulated: con. SubmitChanges ();

Delete data:

This data should be checked before deleting the data.

Querying through IDs This data returns an object

Users u = con. Users.where (r=>r.ids.tostring () ==ids). FirstOrDefault ();

Where parentheses are followed by the lambda expression R for each row of data,=> is the basic format FirstOrDefault () returns the first data or the return null

If u is not empty, the database context is used to delete it.

Con. Users.deleteonsubmit (object);

Con. SubmitChanges ();

Changes to the data:

The modification of the data also requires that the data be checked first and re-assigned the property to be modified for the returned object.

Then commit the change directly.

Con. SubmitChanges ();

Lambda expression

Con. Users.where (r=>r.ids.tostring () ==ids). FirstOrDefault () Return the first data that satisfies the condition or return an empty

If it is a two-condition query, use &&

Con. Users.where (r=>r.username== "Zhangsan" && r.password== "123"). FirstOrDefault ();

"2017-06-01" LINQ base +LAMBDA expression implements additions and deletions to the database

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.