介绍
·Select - Select选择;延迟
·Where - Where查询;延迟
·OrderBy - 按指定表达式对集合正序排序;延迟
·OrderByDescending - 按指定表达式对集合倒序排序;延迟
·GroupBy - 分组;延迟
·Join - Join查询;延迟
·GroupJoin - 分组Join查询;延迟
·以上查询操作符所对应的查询语法
示例
Summary.aspx.cs
using System;
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 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选择;延迟
Summary_Select(); // Where - Where查询;延迟
Summary_Where(); // OrderBy - 按指定表达式对集合正序排序;延迟
// OrderByDescending - 按指定表达式对集合倒序排序;延迟
Summary_OrderBy_OrderByDescending(); // GroupBy - 分组;延迟
Summary_GroupBy(); // Join - Join查询;延迟
Summary_Join(); // GroupJoin - 分组Join查询;延迟
Summary_GroupJoin();
}
}
Select - Select选择;延迟
/// <summary>
/// Select - Select选择;延迟
/// </summary>
void Summary_Select()
{
// 使用Select查询操作符
var categories = _ctx.Categories.Select(
c => new { CategoryName = "类别名称:" + c.CategoryName }); foreach (var c in categories)
{
result.InnerHtml += c.CategoryName + "<br />";
}
result.InnerHtml += "<br />"; // 与上面的Select查询操作符相对应的查询语法
var categories2 = from c in _ctx.Categories
select new { CategoryName = "类别名称:" + c.CategoryName }; foreach (var c in categories2)
{
result.InnerHtml += c.CategoryName + "<br />";
}
result.InnerHtml += "<br />";
}
运行结果
类别名称:Beverages
类别名称:Condiments
类别名称:Confections
类别名称:Dairy Products
类别名称:Grains/Cereals
类别名称:Meat/Poultry
类别名称:Produce
类别名称:Seafood
Where - Where查询;延迟
/// <summary>
/// Where - Where查询;延迟
/// </summary>
void Summary_Where()
{
// 使用Where查询操作符
var ary = _ary.Where(a => a.StartsWith("w") && a.EndsWith("f")); foreach (string s in ary)
{
result.InnerHtml += s + "<br />";
}
result.InnerHtml += "<br />"; // 与上面的Where查询操作符相对应的查询语法
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 />";
}
运行结果
wcf
wpf
wf
OrderBy - 按指定表达式对集合正序排序;延迟
OrderByDescending - 按指定表达式对集合倒序排序;延迟
/// <summary>
/// OrderBy - 按指定表达式对集合正序排序;延迟
/// OrderByDescending - 按指定表达式对集合倒序排序;延迟
/// </summary>
void Summary_OrderBy_OrderByDescending()
{
// 使用OrderBy查询操作符
var ary = (from a in _ary
select a).OrderBy(a => a.Length); // OrderByDescending与OrderBy用法相同 foreach (string s in ary)
{
result.InnerHtml += s + "<br />";
}
result.InnerHtml += "<br />"; // 与上面的OrderBy查询操作符相对应的查询语法
var ary2 = from a in _ary
orderby a.Length ascending // orderby xxx descending与orderby xxx ascending用法相同
select a; foreach (string s in ary2)
{
result.InnerHtml += s + "<br />";
}
result.InnerHtml += "<br />";
}
运行结果
wf
css
wcf
wpf
linq
ssis
ssas
ssrs
xhtml
csharp
asp.net
sqlserver
javascript
silverlight
asp.net ajax
GroupBy - 分组;延迟
/// <summary>
/// GroupBy - 分组;延迟
/// </summary>
void Summary_GroupBy()
{
// 使用GroupBy查询操作符
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 + "个字符:<br />"; foreach (string s in g.Member)
{
result.InnerHtml += "--" + s + "<br />";
}
}
result.InnerHtml += "<br />"; // 与上面的GroupBy查询操作符相对应的查询语法
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 + "个字符:<br />"; foreach (string s in g.Member)
{
result.InnerHtml += "--" + s + "<br />";
}
}
result.InnerHtml += "<br />";
}
运行结果
7个字符:
--asp.net
6个字符:
--csharp
5个字符:
--xhtml
3个字符:
--css
--wcf
--wpf
10个字符:
--javascript
11个字符:
--silverlight
4个字符:
--linq
--ssis
--ssas
--ssrs
2个字符:
--wf
9个字符:
--sqlserver
12个字符:
--asp.net ajax
Join - Join查询;延迟
/// <summary>
/// Join - Join查询;延迟
/// </summary>
void Summary_Join()
{
// 使用Join查询操作符
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查询操作符相对应的查询语法
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 />";
运行结果
Beverages - Chai
Beverages - Chang
Condiments - Aniseed Syrup
Condiments - Chef Anton's Cajun Seasoning
Condiments - Chef Anton's Gumbo Mix
GroupJoin - 分组Join查询;延迟
/// <summary>
/// GroupJoin - 分组Join查询;延迟
/// </summary>
void Summary_GroupJoin()
{
// 使用GroupJoin查询操作符
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 />"; // 与上面的GroupJoin查询操作符相对应的查询语法
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 />";
}
运行结果
Beverages:12
Condiments:12
Confections:13
Dairy Products:10
Grains/Cereals:7
Meat/Poultry:6
Produce:5
Seafood:12