(转)Linq-查询操作符之SelectWhereOrderByOrderByDescending
Posted mingsonzheng
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了(转)Linq-查询操作符之SelectWhereOrderByOrderByDescending相关的知识,希望对你有一定的参考价值。
介绍
Select - 选择;延迟
Where - 查询;延迟
OrderBy - 按指定表达式对集合正序排序;延迟
OrderByDescending - 按指定表达式对集合倒序排序;延迟
GroupBy - 分组;延迟
Join - 查询;延迟
GroupJoin - 分组Join查询;延迟
以上查询操作符所对应的查询语法
示例
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();
}
}
/// <summary>
/// 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
/// <summary>
/// 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
/// <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
/// <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
/// <summary>
/// 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
/// <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
原文链接:
http://www.cnblogs.com/qiuweiguo/archive/2011/08/03/2126600.html
以上是关于(转)Linq-查询操作符之SelectWhereOrderByOrderByDescending的主要内容,如果未能解决你的问题,请参考以下文章
LINQ to SQL语句之Select/Distinct和Count/Sum/Min/Max/Avg (转)
LINQ查询操作符之FirstFirstOrDefaultLastLastOrDefaultElementAtElementAtOrDefaultContainsAnyAllCoun
LINQ查询操作符之FirstFirstOrDefaultLastLastOrDefaultElementAtElementAtOrDefaultContainsAnyAllCoun
LINQ查询操作符之FirstFirstOrDefaultLastLastOrDefaultElementAtElementAtOrDefaultContainsAnyAllCoun