LINQ base +LAMBDA expressions for database additions and deletions and simple queries

Source: Internet
Author: User

One, LINQ to SQL class

High-integrated database access technology

LINQ can be used instead of the previous ADO, eliminating the massive work of the entity classes and data access classes that you knock code

Entity class:

Add a LINQ to SQL class----Select the database to use----drag into the table you want to use to save immediately is equivalent to creating an entity class

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's the equivalent of querying the whole method.

Ii. using LINQ to perform related operations on databases

1. Add Data

Each piece of data is an entity class object. Instantiate it first, and then assign a value to each property of the object.

To add data operations in a data access class

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

Commit changes to the database after the execution of the method: Con. SubmitChanges ();

2. 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 a lambda expression, R represents a Users object,=> is the basic format, FirstOrDefault () returns the first data or the return of an empty

If u is not empty, it is deleted in the data access class.

Con. Users.deleteonsubmit (object);

Con. SubmitChanges ();

3. Data modification

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.

Last con. SubmitChanges ();

4. Simple data query

Directly in the data access class according to the object's properties to query, multiple query conditions in the middle with && connection;

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

LINQ base +LAMBDA expression to database additions and deletions and simple queries

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.