Wind shadows ASP. NET basics 15 LINQ to sql2

Source: Internet
Author: User
UsingSystem;
 
UsingSystem. Collections. Generic;
 
UsingSystem. LINQ;
 
UsingSystem. Web;
 
UsingSystem. Web. UI;
 
UsingSystem. Web. UI. webcontrols;
 
 
 
NamespaceWebapplication1
 
{
 
Public Partial ClassLinq2: system. Web. UI. Page
 
{
 
Protected VoidPage_load (ObjectSender, eventargs E)
{
 
# RegionJoin
 
/*
 
 
 
* Join Operation
 
 
 
Applicable scenarios: we have one-to-one relationships, one-to-many relationships, and many-to-many relationships in Table relationships. For the relationship between tables, you can use these to operate on multiple tables.
 
 
 
Note: The join operations include join (join query), selectmany (select one-to-multiple choice), and groupjoin (Group join query ).
 
This extension method performs the inner Join Operation on the elements matching the middle keys in the two sequences.
 
 
 
* Selectworkflow
 
 
Note: When writing a query statement, if it is translated as selectmany, two conditions must be met. 1: The query statement does not contain join and into. 2: entityset must appear. In our table, there are one-to-one, one-to-many, and many-to-many relationships. The following describes them respectively.
 
 
 
1. One-to-many relationship (1 to multiple ):
 
VaR q =
 
From C in db. MERs
 
From o in C. Orders
 
Where C. City = "London"
 
Select O;
Statement Description: MERs has a one-to-multiple relationship with orders. That is, orders appears in entityset form in the MERs class. Therefore, the second from is filtered from C. Orders instead of DB. Orders. In this example, use the foreign key navigation in the from clause to select all orders of London customers.
 
VaR q =
 
From P in db. Products
 
Where p. Supplier. Country = "USA" & P. unitsinstock = 0
 
Select P;
 
Statement Description: This statement uses the p. Supplier. Country condition and indirectly associates with the supplier table. In this example, the where clause uses a foreign key navigation bar to filter products whose suppliers are out of stock in the United States.
 
*/
 
# Endregion
 
 
# RegionOrderby
 
/*
 
 
 
* Order by Operation
 
 
 
Applicable scenarios: sort the queried statements, such as by time.
 
 
 
Note: sort the set by the specified expression; delay, in ascending order by default, and descending indicates descending order. The corresponding extension methods are orderby and orderbydescending.
 
 
 
1. Simple Form
 
 
 
In this example, orderby is used to sort employees by employment date:
VaR q =
 
From E in db. Employees
 
Orderby E. hiredate
 
Select E;
 
Note: The default value is ascending.
 
 
 
2. Conditional form
 
 
 
Note: The order of where and order by is not important. In T-SQL, where and order by have strict location restrictions.
 
VaR q =
 
From o in db. Orders
Where o. shipcity = "London"
 
Orderby O. Freight
 
Select O;
 
*
 
Select * from orders where shipcity = 'London 'order by freight
 
*
 
Statement Description: use where and orderby to sort by freight.
 
 
 
3. Sort in descending order
 
VaR q =
 
From P in db. Products
Orderby P. unitprice descending
 
Select P;
 
4. thenby
 
 
 
Statement Description: sort the customer by using the compound orderby statement:
 
VaR q =
 
From C in db. MERs
 
Orderby C. City, C. contactname
 
Select C;
 
Sort by multiple expressions. For example, sort by city first. When the city is the same, sort by contactname. Use this sentence
Lambda expressions are written like this:
 
VaR q =
 
DB. MERs
 
. Orderby (C => C. City)
 
. Thenby (C => C. contactname). tolist ();
 
There is no thenby statement in the T-SQL, it is still translated as orderby, so you can also use the following statement to express:
 
VaR q =
 
DB. MERs
 
. Orderby (C => C. contactname)
 
. Orderby (C => C. City). tolist ();
Note that when multiple orderby operations are performed, the cascade mode is in reverse order. For descending order, replace it with the corresponding descending operator.
 
VaR q =
 
DB. MERs
 
. Orderbydescending (C => C. City)
 
. Thenbydescending (C => C. contactname). tolist ();
 
 
 
*/
 
# Endregion
 
 
 
 
 
}
 
}
 
}
Related Article

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.