風影ASP.NET基礎教育15 LINQ TO SQL 2
9644 ワード
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace WebApplication1
{
public partial class Linq2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
#region Join
/*
* Join
: , , 。 , 。
: Join , Join(Join ), SelectMany(Select ) GroupJoin( Join )。
inner join
* SelectMany
: , SelectMany 2 。1: join into,2: EntitySet。 , , , 。
1. (1 to Many):
var q =
from c in db.Customers
from o in c.Orders
where c.City == "London"
select o;
:Customers Orders 。 Orders Customers EntitySet 。 from c.Orders db.Orders 。 From 。
var q =
from p in db.Products
where p.Supplier.Country == "USA" && p.UnitsInStock == 0
select p;
: p.Supplier.Country , Supplier 。 Where 。
*/
#endregion
#region OrderBy
/*
* Order By
: , 。
: ; ,: ; , , descending , OrderBy OrderByDescending
1.
orderby :
var q =
from e in db.Employees
orderby e.HireDate
select e;
:
2.
:Where Order By 。 T-SQL ,Where Order By 。
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
*
: where orderby 。
3.
var q =
from p in db.Products
orderby p.UnitPrice descending
select p;
4.ThenBy
: orderby , :
var q =
from c in db.Customers
orderby c.City, c.ContactName
select c;
: , City , City , ContactName 。
Lambda :
var q =
db.Customers
.OrderBy(c => c.City)
.ThenBy(c => c.ContactName).ToList();
T-SQL ThenBy , OrderBy, :
var q =
db.Customers
.OrderBy(c => c.ContactName)
.OrderBy(c => c.City).ToList();
, OrderBy , 。 , 。
var q =
db.Customers
.OrderByDescending(c => c.City)
.ThenByDescending(c => c.ContactName).ToList();
*/
#endregion
}
}
}
.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }