Lecture 4th: query expression LINQ (2)

Source: Internet
Author: User

2007.7.20 Li Jianzhong

Introduction to LINQ to SQL

Object-Oriented Domain:

All objects (fields, methods, references ...)

Relational Database:

Everything is centered on table data (rows, columns, and relationships)

Linq to SQL integrates SQL schema information into CLR metadata, and naturally converts objects in the object-oriented field to table data in relational databases, this allows us to use LINQ to easily access relational databases.

As an important component of ADO. NET.

 

Object Class ing

Note that the [Table] feature corresponds to the Table named "Category" in the database. The [Column] feature corresponds to the columns in the database. If the attribute or field is not marked, the database will not ignore this attribute or field.

 

DataContext data context object

Note that DataContext is very similar to a database link object, but it provides more support for Linq to SQL.

 

Query by using Linq to SQL

When the Form is loaded, all category information is displayed.

MSPetShop is a strong-type DataContext. We recommend that you use a strong-type DataContext for Linq to SQL.

We can directly call. Categorys for Strongly typed DataContext to make it easier to use.

In this way, the queried c is a strongly typed object, because it is already the result of the ing from the database to the object.

In the past, this method was not an object method, but a combination of row and column information. In addition, the Select statement needs to be checked in the database server. Now, any syntax errors will be directly checked by the C # compiler, which ensures that most of the careless syntax errors are parsed.

 

Define basic relationships

Note that Products maps the one-to-multiple relationship between Category and Product. CategoryId is the primary key for the Category table and a foreign key for the Product table.

Note that Category maps the one-to-multiple relationship between Product and Category.

 

Linq to SQL relational Programming

EntitySet is a set relation and EntityRef is a reference relation.

This is an association query for two tables. First, check a specific column in Category, and then find whether a item in the Product list in this column is selected and select this Product.

 

Change Data Source

Note that SubmitChanges () has changed the Object layer to the data layer.

 

Change the data source using Linq to SQL

2010.11.12

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.