Step-by-step Camp VS 2008 +. NET 3.5 (5) – LINQ query operators Select, Where, OrderBy, the OrderByDescending GroupBy, Join, GroupJoin

Step-by-step Camp VS 2008 +. NET 3.5 (5) – LINQ query operators Select, Where the OrderBy, the OrderByDescending, and the GroupBy, Join, GroupJoin and its corresponding query syntax

Author:

Introduction
· Select – Select options; delay
Where – Where query; delay
OrderBy – sort of a collection of positive sequence specified expression; delay
The · the OrderByDescending delay – by the specified expression to the collection in reverse order;
· GroupBy – grouping; delay
· Join – Join query; delay
GroupJoin – Group Join query; delay
The above query operator corresponding to the query syntax

Example
Summary.aspx.cs

usingSystem;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;using System.Collections.Generic;
using DAL;

public partial class LINQ_Summary: System.Web.UI.Page
{
NorthwindDataContext _ctx =   new the NorthwindDataContext ();
string [] _ary =   null;

protected   void Page_Load (object sender, EventArgs e)
{
_ary =   new   string [] {“Asp.net”, “csharp”, “xhtml”, “css”, “javascript”,
“Wcf”, “wpf”, “silverlight”, “linq”, “wf”,
“Sqlserver”, “asp.net ajax”, “ssis”, “ssas”, “ssrs”};

/ / Select – Select options; delay
Summary_Select ();

/ / Where – Where inquiries; delay
Summary_Where ();

/ / OrderBy – sort of a collection of positive sequence specified expression; delay
/ / The the OrderByDescending – expression specified collection reverse order; delay
Summary_OrderBy_OrderByDescending ();

/ / GroupBy – grouping; delay
Summary_GroupBy ();

/ / Join – Join query; delay
Summary_Join ();

/ / The GroupJoin – Group Join query; delay
Summary_GroupJoin ();
}
}

Select – Select options; delay

    / ** / / / /   <summary>
/ / / Select – Select options; delay
/ / /   </ Summary>
void Summary_Select ()
{
/ / Use the Select query operators
var categories = _ctx.Categories.Select (
c =>   new   {CategoryName =   Category Name:   The + c.CategoryName}); 

foreach (var c in categories)
{
result.InnerHtml + = c.CategoryName +   <br /> “;
}
result.InnerHtml + =   <br /> “; 

/ / Above the Select query operators corresponding query syntax
var categories2 = from c in _ctx.Categories
select new   {CategoryName =   Category Name:   + C.CategoryName}; 

foreach (var c in categories2)
{
result.InnerHtml + = c.CategoryName +   <br /> “;
}
result.InnerHtml + =   <br /> “;
}

The result
Category Name: Beverages
Category Name: Condiments
Category Name: Confections
Category: Dairy Products
Category Name: Grains / Cereals
Category Name: Meat / Poultry
Category Name: Produce
Category Name: Seafood

Where – Where inquiries; delay

    / ** / / / /   <summary>
/ / / Where – Where inquiries; delay
/ / /   </ Summary>
void Summary_Where ()
{
/ / Where query operators
var ary = _ary.Where (a => a.StartsWith (“w”) && a.EndsWith (“f”)); 

foreach (string s in ary)
{
result.InnerHtml + = s +   <br /> “;
}
result.InnerHtml + =   <br /> “; 

/ / Where the above query operators corresponding to the query syntax
var ary2 = from a in _ary
where a.StartsWith (“w”) && a.EndsWith (“f”)
select a; 

foreach (string s in ary2)
{
result.InnerHtml + = s +   <br /> “;
}
result.InnerHtml + =   <br /> “;
}

The result
wcf
wpf
wf

OrderBy – sort of a collection of positive sequence specified expression; delay
The OrderByDescending – by the specified expression to the collection in reverse order; delay

    / ** / / / /   <summary>
/ / / OrderBy – sort of a collection of positive sequence specified expression; delay
/ / / The OrderByDescending – the expression specified collection reverse order; delay
/ / /   </ Summary>
void Summary_OrderBy_OrderByDescending ()
{
/ / OrderBy query operators.
var ary = (from a in _ary
select a). OrderBy (a => a.Length); / / OrderByDescending OrderBy usage of the same 

foreach (string s in ary)
{
result.InnerHtml + = s +   <br /> “;
}
result.InnerHtml + =   <br /> “; 

/ / OrderBy above, query operator corresponding to the query syntax
var ary2 = from a in _ary
the orderby a.Length ascending / / orderby xxx descending and the orderby xxx Ascending usage
select a; 

foreach (string s in ary2)
{
result.InnerHtml + = s +   <br /> “;
}
result.InnerHtml + =   <br /> “;
}

The result
wf
css
wcf
wpf
linq
ssis
ssas
ssrs
xhtml
csharp
asp.net
sqlserver
javascript
silverlight
asp.net ajax

GroupBy – grouping; delay

    / ** / / / /   <summary>
/ / / GroupBy – grouping; delay
/ / /   </ Summary>
the void Summary_GroupBy ()
{
/ / Use the GroupBy query operators
var list = (from a in _ary
select a). GroupBy (a => a.length) Select (
g =>   new   {Group = g.Key, Member = g}); 

foreach (var g in list)
{
result.InnerHtml + = g.Group +   “Characters: <br />”; 

foreach (string the s in g.Member)
{
result.InnerHtml + =   “-“   + S +   <br /> “;
}
}
result.InnerHtml + =   <br /> “; 

/ / GroupBy above, query operator corresponding to the query syntax
var list2 = from a in _ary
group a by a.Length into g
select new   {Group = g.Key, Member = g}; 

foreach (var g in list2)
{
result.InnerHtml + = g.Group +   “Characters: <br />”; 

foreach (string the s in g.Member)
{
result.InnerHtml + =   “-“   + S +   <br /> “;
}
}
result.InnerHtml + =   <br /> “;
}

The result
Seven characters:
– Asp net
6 characters:
– CSharp
5 characters:
– XHTML
Three characters:
– CSS
– WCF
– WPF
10 characters:
– Javascript
11 characters:
– Silverlight
4 characters:
– LINQ
– SSIS
– SSAS
– SSRS
2 characters:
– Wf
9 characters:
– Sqlserver
12 characters:
– Asp net ajax

Join – Join query; delay

    / ** / / / /   <summary>
/ / / Join – Join query; delay
/ / /   </ Summary>
void Summary_Join ()
{
/ / Use the Join query operators
var products = _ctx.Products.Join (
_ctx.Categories
p => p.CategoryID,
c => c.CategoryID
(P, c) =>   new   {C.CategoryName, p.ProductName}). Take (5); 

foreach (var p in products)
{
result.InnerHtml + = p.CategoryName +   “-“   + P.ProductName +   <br /> “;
}
result.InnerHtml + =   <br /> “; 

/ / Join the above query operators corresponding query syntax
var products2 = (from p in _ctx.Products
join c in _ctx.Categories
on p.CategoryID equals c.CategoryID
select new   {C.CategoryName, p.ProductName}). Take (5); 

foreach (var p in products2)
{
result.InnerHtml + = p.CategoryName +   “-“   + P.ProductName +   <br /> “;
}
result.InnerHtml + =   <br /> “;
}

The result
Beverages – Chai
Beverages – Chang
Condiments – Aniseed Syrup
Condiments – Chef Anton’s Cajun Seasoning
Condiments – Chef Anton’s Gumbo Mix

The GroupJoin – grouping and Join queries; delay

    / ** / / / /   <summary>
/ / / GroupJoin – Group Join query; delay
/ / /   </ Summary>
void Summary_GroupJoin ()
{
/ / Use the GroupJoin query operators
var products = _ctx.Categories.GroupJoin (
_ctx.Products
c => c.CategoryID
p => p.CategoryID,
(P, g) =>   new   {P.CategoryName, ProductCount = g.Count ()}); 

foreach (var g in Products)
{
result.InnerHtml + = g.CategoryName +   “:”   + G.ProductCount +   <br /> “; 

}
result.InnerHtml + =   <br /> “; 

/ / The GroupJoin with the above query operators corresponding query syntax
var products2 = from c in _ctx.Categories
join p in _ctx.Products on c.CategoryID equals p.CategoryID into g
select new   {CategoryName = c.CategoryName, ProductCount = g.Count ()}; 

foreach (var g in products2)
{
result.InnerHtml + = g.CategoryName +   “:”   + G.ProductCount +   <br /> “; 

}
result.InnerHtml + =   <br /> “;
}

The result
Beverages: 12
Condiments: 12
Confections: 13
Dairy Products: 10
Grains / Cereals: 7
Meat / Poultry: 6
Produce: 5
Seafood: 12

OK

Posted by databasesql