.NET 3.5(5) - LINQ查询操作符之Select、Where、OrderBy、OrderByDescending

.NET 3.5(5) - LINQ查询操作符之Select、Where、OrderBy、OrderByDescending

介绍
    ·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

时间: 2024-11-12 20:51:16

.NET 3.5(5) - LINQ查询操作符之Select、Where、OrderBy、OrderByDescending的相关文章

LINQ查询操作符之Select、Where、OrderBy、OrderByDescending、GroupBy、Join、GroupJoin及其对应的查询语法

介绍    ·Select - Select选择:延迟    ·Where - Where查询:延迟    ·OrderBy - 按指定表达式对集合正序排序:延迟    ·OrderByDescending - 按指定表达式对集合倒序排序:延迟    ·GroupBy - 分组:延迟    ·Join - Join查询:延迟    ·GroupJoin - 分组Join查询:延迟    ·以上查询操作符所对应的查询语法 示例Summary.aspx.cs using System; using

LINQ查询操作符

一.投影操作符 1. Select Select操作符对单个序列或集合中的值进行投影.下面的示例中使用select从序列中返回Employee表的所有列: using (NorthwindDataContext db=new NorthwindDataContext()) { //查询语法 var query = from e in db.Employees where e.FirstName.StartsWith("M") select e; //方法语法 var q = db.Em

LINQ查询操作符之First、FirstOrDefault、Last、LastOrDefault、ElementAt、ElementAtOrDefault、Contains、Any、All、Count 等

介绍 ·First - 返回集合中的第一个元素:不延迟 ·FirstOrDefault - 返回集合中的第一个元素(如果没有则返回默认值):不延迟 ·Last - 返回集合中的最后一个元素:不延迟 ·LastOrDefault - 返回集合中的最后一个元素(如果没有则返回默认值) ·ElementAt - 返回集合中指定索引的元素:不延迟 ·ElementAtOrDefault - 返回集合中指定索引的元素(如果没有则返回默认值):不延迟 ·Contains - 判断集合中是否包含有某一元素:不

&lt;转&gt;LINQ查询操作符之First、FirstOrDefault、Last、LastOrDefault、ElementAt、ElementAtOrDefault、Contains、Any、All、Coun

介绍 ·First - 返回集合中的第一个元素:不延迟 ·FirstOrDefault - 返回集合中的第一个元素(如果没有则返回默认值):不延迟 ·Last - 返回集合中的最后一个元素:不延迟 ·LastOrDefault - 返回集合中的最后一个元素(如果没有则返回默认值) ·ElementAt - 返回集合中指定索引的元素:不延迟 ·ElementAtOrDefault - 返回集合中指定索引的元素(如果没有则返回默认值):不延迟 ·Contains - 判断集合中是否包含有某一元素:不

(转)LINQ查询操作符之Distinct、Union、Concat、Intersect、Except

介绍 ·Distinct - 过滤集合中的相同项:延迟 ·Union - 连接不同集合,自动过滤相同项:延迟 ·Concat - 连接不同集合,不会自动过滤相同项:延迟 ·Intersect - 获取不同集合的相同项(交集):延迟 ·Except - 从某集合中删除其与另一个集合中相同的项:延迟 ·Skip - 跳过集合的前n个元素:延迟 ·Take - 获取集合的前n个元素:延迟 ·SkipWhile - 直到某一条件成立就停止跳过:延迟 ·TakeWhile - 直到某一条件成立就停止获取:

C#编程(六十二)---------LINQ标准的查询操作符

LINQ标准的查询操作符 首先我们来看一下LINQ的操作符,可根据查询操作符的操作"类型"进行分类,如把它们分成投影,限制,排序,联接,分组,串联,聚合,集合,生成,转换,元素,相等,量词,分割等. 类型 操作符名称 投影操作符 Select,SelectMany 限制操作符 Where 排序操作符 OrderBy,OrderByDescending,ThenBy,ThenByDescending,Reverse 联接操作符 Join,GroupJoin 分组操作符 GroupBy 串

C#高级编程六十二天----LINQ标准的查询操作符

LINQ标准的查询操作符 首先我们来看一下LINQ的操作符,可根据查询操作符的操作"类型"进行分类,如把它们分成投影,限制,排序,联接,分组,串联,聚合,集合,生成,转换,元素,相等,量词,分割等. 类型 操作符名称 投影操作符 Select,SelectMany 限制操作符 Where 排序操作符 OrderBy,OrderByDescending,ThenBy,ThenByDescending,Reverse 联接操作符 Join,GroupJoin 分组操作符 GroupBy 串

LINQ标准查询操作符详解(转)

 一. 关于LINQ       LINQ 英文全称是“Language-Integrated Query”,中文为“语言集成查询”,它是微软首席架构师.Delphi 之父和C# 之父——Anders Hejlsberg 提出的并由其团队着力打造的一组用于c#和Visual Basic语言的扩展,为 C# 和 Visual Basic 语言语法提供强大的查询功能.微软从2003年开始启动LINQ的开发,在VisualStudio2008中开始加入LINQ功能. LINQ提供的便利: 1)使用一种

Linq to OBJECT延时标准查询操作符

1.Where 操作符用于限定输入集合中的元素,将符合条件的元素组织声称一个序列结果. 2.Select  操作符用于根据输入序列中的元素创建相应的输出序列中的元素,输出序列中的元素类型可以与输入序列中的元素类型相同,也可以不同.下面来看看Select方法的原型. 3.SelectMany 操作符用于根据输入序列中的每一个元素,在输出序列中创建相应的零个或者多个元素,与Select操作符不同,Select操作符会根据输入序列中的每一个元素创建一个对应的输出序列元素,而SelectMany操作符可