Simple use of linq to SQL

Source: Internet
Author: User

1. Create a winform project.

2. Add a Linq to SQL class named Northwind

3. Open the server resource manager and drag the table to the linq to SQL class. The entity class is generated by.

4. instantiate the DataContext class

NorthwindDataContext dc = new NorthwindDataContext();

5. Now you can use Linq to SQL.

A. query all Employees data

Var ee =
From employee in dc. Employees
Select employee;

B. Add a Where condition to query all cities = London in the Employee table.

             e2 =                 employee  employee.City ==                  employee;

 

C. query the data of EmployeeID <5

             e3 =                 employee  employee.EmployeeID <                  employee;

 

D. query the employees whose employment date is before January 1, January 1, 1993.

             e4 =                 employee  employee.HireDate <  DateTime(, ,  employee;

 

E. Multiple query Conditions

             e5 =                 employee  employee.HireDate <  DateTime(, ,  employee.EmployeeID <                  employee;

 

F. like statement

 e6 =                 employee  SqlMethods.Like(employee.FirstName,                  employee;

 

G. query the specified Column

 e7 = employee                   ===

 

H. subquery

             e8 =                 employee  employee.Orders.Count >                  employee;

 

I. Internal Connection

             e9 =                 cus  cus.City ==                  ord;

 

J. Add

            Employees m = = = = 

 

K. Modify

             e10 = dc.Employees.FirstOrDefault(employee => employee.LastName == = 

 

L. Delete

             e10 = dc.Employees.FirstOrDefault(employee => employee.LastName == 

 

The exercise code uses Vs2012 and the Microsoft northwnd sample database.

Code download: http://pan.baidu.com/s/1pJyh0Mn

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.