LINQ Study Notes (3): Where

Source: Internet
Author: User
I have learned the basic knowledge of the datacontext of LINQ. You may think it is very troublesome to manually define the object classes corresponding to tables in the database. Don't worry, vs2008 provides a tool to automatically generate object classes and relationships. Next we will use vs2008 to create datacontext. First, right-click the project, select Add new item, and then select "LINQ to SQL class", and name it datacity. dbml. Open datacity. dbml. The text in the design view shows that you can drag an item from the server resource manager or attack box to the design interface to create an object class. In the server resource manager, create a data connection pointing to the datacity database, drag the db_city table to the design view of datacity. dbml, and press Ctrl + S to save the data. Open datacity. Designer. CS and you will find that the system automatically creates a dbing for the db_city table in the datacity database. Next, let's start with the LINQ to SQL statement to get a full understanding of the LINQ. Let's start with the simplest where Statement, which is also the most commonly used in programming.

Where:Similar to the WHERE clause in SQL commands, it is used to define the scope, that is, filter, and the judgment condition is the clause followed by it. The where operation includes three forms: simple form, relational condition form, and first () form. The following example uses an instance:
1. Simple Form: var citylist =
From city in DC. db_city
Where city. PID = 0
Select city;

Corresponding SQL statement: select [t0]. [cityid], [t0]. [pid], [t0]. [cityname], [t0]. [isvisible], [t0]. [islastnode], [t0]. [order] as [order], [t0]. [metaid], [t0]. [citytype], [t0]. [listname]
From [DBO]. [db_city] as [t0]
Where [t0]. [pid] = @ P0
-- @ P0: Input int (size = 0; prec = 0; scale = 0) [0]

2. Link Condition form:

VaR citylist =
From city in DC. db_city
Where city. PID = 0 & City. isvisible = true
Select city;

Corresponding SQL statement: select [t0]. [cityid], [t0]. [pid], [t0]. [cityname], [t0]. [isvisible], [t0]. [islastnode], [t0]. [order] as [order], [t0]. [metaid], [t0]. [citytype], [t0]. [listname]
From [DBO]. [db_city] as [t0]
Where ([t0]. [pid] = @ P0) and ([t0]. [isvisible] = @ P1)
-- @ P0: Input int (size = 0; prec = 0; scale = 0) [0]
-- @ P1: Input int (size = 0; prec = 0; scale = 0) [1]

VaR citylist =
From city in DC. db_city
Where city. PID = 0 | city. citytype = 2
Select city;

Corresponding SQL statement: select [t0]. [cityid], [t0]. [pid], [t0]. [cityname], [t0]. [isvisible], [t0]. [islastnode], [t0]. [order] as [order], [t0]. [metaid], [t0]. [citytype], [t0]. [listname]
From [DBO]. [db_city] as [t0]
Where ([t0]. [pid] = @ P0) or ([t0]. [citytype] = @ P1)
-- @ P0: Input int (size = 0; prec = 0; scale = 0) [0]
-- @ P1: Input int (size = 0; prec = 0; scale = 0) [2]

3. First: return an element in the set. The essence is to add top (1) to the SQL statement ). VaR citylist = Dc. db_city.first ();

Corresponding SQL statement: Select top (1) [t0]. [cityid], [t0]. [pid], [t0]. [cityname], [t0]. [isvisible], [t0]. [islastnode], [t0]. [order] as [order], [t0]. [metaid], [t0]. [citytype], [t0]. [listname]
From [DBO]. [db_city] as [t0]

VaR citylist = Dc. db_city.first (city => city. PID = 0 );

Corresponding SQL statement: Select top (1) [t0]. [cityid], [t0]. [pid], [t0]. [cityname], [t0]. [isvisible], [t0]. [islastnode], [t0]. [order] as [order], [t0]. [metaid], [t0]. [citytype], [t0]. [listname]
From [DBO]. [db_city] as [t0]
Where [t0]. [pid] = @ P0
-- @ P0: Input int (size = 0; prec = 0; scale = 0) [0]

Well, I will learn this today and continue to take the time to study tomorrow.

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.