步步为营VS 2008 + .NET 3.5(5) - LINQ查询操作符之Select、Where、OrderBy、OrderByDescending-阿里云开发者社区

开发者社区> 技术小胖子> 正文

步步为营VS 2008 + .NET 3.5(5) - LINQ查询操作符之Select、Where、OrderBy、OrderByDescending

简介:
+关注继续查看
[索引页]
[源码下载]


步步为营VS 2008 + .NET 3.5(5) - LINQ查询操作符之Select、Where、OrderBy、OrderByDescending、GroupBy、Join、GroupJoin及其对应的查询语法


作者:webabcd


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


示例
Summary.aspx.cs
InBlock.gifusing System; 
InBlock.gifusing System.Data; 
InBlock.gifusing System.Configuration; 
InBlock.gifusing System.Collections; 
InBlock.gifusing System.Linq; 
InBlock.gifusing System.Web; 
InBlock.gifusing System.Web.Security; 
InBlock.gifusing System.Web.UI; 
InBlock.gifusing System.Web.UI.WebControls; 
InBlock.gifusing System.Web.UI.WebControls.WebParts; 
InBlock.gifusing System.Web.UI.HtmlControls; 
InBlock.gifusing System.Xml.Linq; 
InBlock.gif 
InBlock.gifusing System.Collections.Generic; 
InBlock.gifusing DAL; 
InBlock.gif 
InBlock.gifpublic partial class LINQ_Summary : System.Web.UI.Page 
InBlock.gif
InBlock.gif        NorthwindDataContext _ctx = new NorthwindDataContext(); 
InBlock.gif        string[] _ary = null
InBlock.gif 
InBlock.gif        protected void Page_Load(object sender, EventArgs e) 
InBlock.gif        { 
InBlock.gif                _ary = new string[] { "asp.net""csharp""xhtml""css""javascript",    
InBlock.gif                        "wcf""wpf""silverlight""linq""wf",    
InBlock.gif                        "sqlserver""asp.net ajax""ssis""ssas""ssrs" }; 
InBlock.gif 
InBlock.gif                // Select - Select选择;延迟 
InBlock.gif                Summary_Select(); 
InBlock.gif 
InBlock.gif                // Where - Where查询;延迟 
InBlock.gif                Summary_Where(); 
InBlock.gif 
InBlock.gif                // OrderBy - 按指定表达式对集合正序排序;延迟 
InBlock.gif                // OrderByDescending - 按指定表达式对集合倒序排序;延迟 
InBlock.gif                Summary_OrderBy_OrderByDescending(); 
InBlock.gif 
InBlock.gif                // GroupBy - 分组;延迟 
InBlock.gif                Summary_GroupBy(); 
InBlock.gif 
InBlock.gif                // Join - Join查询;延迟 
InBlock.gif                Summary_Join(); 
InBlock.gif 
InBlock.gif                // GroupJoin - 分组Join查询;延迟 
InBlock.gif                Summary_GroupJoin(); 
InBlock.gif        } 
InBlock.gif}
 
Select - Select选择;延迟
/// <summary> 
InBlock.gif        /// Select - Select选择;延迟 
InBlock.gif        /// </summary> 
InBlock.gif        void Summary_Select() 
InBlock.gif        { 
InBlock.gif                // 使用Select查询操作符 
InBlock.gif                var categories = _ctx.Categories.Select( 
InBlock.gif                        c => new { CategoryName = "类别名称:" + c.CategoryName }); 
InBlock.gif 
InBlock.gif                foreach (var c in categories) 
InBlock.gif                { 
InBlock.gif                        result.InnerHtml += c.CategoryName + "<br />"
InBlock.gif                } 
InBlock.gif                result.InnerHtml += "<br />"
InBlock.gif 
InBlock.gif 
InBlock.gif                // 与上面的Select查询操作符相对应的查询语法 
InBlock.gif                var categories2 = from c in _ctx.Categories 
InBlock.gif                                                    select new { CategoryName = "类别名称:" + c.CategoryName }; 
InBlock.gif 
InBlock.gif                foreach (var c in categories2) 
InBlock.gif                { 
InBlock.gif                        result.InnerHtml += c.CategoryName + "<br />"
InBlock.gif                } 
InBlock.gif                result.InnerHtml += "<br />"
InBlock.gif        }
 
运行结果
类别名称:Beverages
类别名称:Condiments
类别名称:Confections
类别名称:Dairy Products
类别名称:Grains/Cereals
类别名称:Meat/Poultry
类别名称:Produce
类别名称:Seafood

Where - Where查询;延迟
/// <summary> 
InBlock.gif        /// Where - Where查询;延迟 
InBlock.gif        /// </summary> 
InBlock.gif        void Summary_Where() 
InBlock.gif        { 
InBlock.gif                // 使用Where查询操作符 
InBlock.gif                var ary = _ary.Where(a => a.StartsWith("w") && a.EndsWith("f")); 
InBlock.gif 
InBlock.gif                foreach (string s in ary) 
InBlock.gif                { 
InBlock.gif                        result.InnerHtml += s + "<br />"
InBlock.gif                } 
InBlock.gif                result.InnerHtml += "<br />"
InBlock.gif 
InBlock.gif 
InBlock.gif                // 与上面的Where查询操作符相对应的查询语法 
InBlock.gif                var ary2 = from a in _ary 
InBlock.gif                                     where a.StartsWith("w") && a.EndsWith("f"
InBlock.gif                                     select a; 
InBlock.gif 
InBlock.gif                foreach (string s in ary2) 
InBlock.gif                { 
InBlock.gif                        result.InnerHtml += s + "<br />"
InBlock.gif                } 
InBlock.gif                result.InnerHtml += "<br />"
InBlock.gif        }
 
运行结果
wcf
wpf
wf

OrderBy - 按指定表达式对集合正序排序;延迟
OrderByDescending - 按指定表达式对集合倒序排序;延迟
InBlock.gif /// <summary> 
InBlock.gif        /// OrderBy - 按指定表达式对集合正序排序;延迟 
InBlock.gif        /// OrderByDescending - 按指定表达式对集合倒序排序;延迟 
InBlock.gif        /// </summary> 
InBlock.gif        void Summary_OrderBy_OrderByDescending() 
InBlock.gif        { 
InBlock.gif                // 使用OrderBy查询操作符 
InBlock.gif                var ary = (from a in _ary 
InBlock.gif                                     select a).OrderBy(a => a.Length); // OrderByDescending与OrderBy用法相同 
InBlock.gif 
InBlock.gif                foreach (string s in ary) 
InBlock.gif                { 
InBlock.gif                        result.InnerHtml += s + "<br />"
InBlock.gif                } 
InBlock.gif                result.InnerHtml += "<br />"
InBlock.gif 
InBlock.gif 
InBlock.gif                // 与上面的OrderBy查询操作符相对应的查询语法 
InBlock.gif                var ary2 = from a in _ary 
InBlock.gif                                     orderby a.Length ascending // orderby xxx descending与orderby xxx ascending用法相同 
InBlock.gif                                     select a; 
InBlock.gif 
InBlock.gif                foreach (string s in ary2) 
InBlock.gif                { 
InBlock.gif                        result.InnerHtml += s + "<br />"
InBlock.gif                } 
InBlock.gif                result.InnerHtml += "<br />"
InBlock.gif        }
 
运行结果
wf
css
wcf
wpf
linq
ssis
ssas
ssrs
xhtml
csharp
asp.net
sqlserver
javascript
silverlight
asp.net ajax

GroupBy - 分组;延迟
/// <summary> 
InBlock.gif        /// GroupBy - 分组;延迟 
InBlock.gif        /// </summary> 
InBlock.gif        void Summary_GroupBy() 
InBlock.gif        { 
InBlock.gif                // 使用GroupBy查询操作符 
InBlock.gif                var list = (from a in _ary 
InBlock.gif                                        select a).GroupBy(a => a.Length).Select( 
InBlock.gif                                                g => new { Group = g.Key, Member = g }); 
InBlock.gif 
InBlock.gif                foreach (var g in list) 
InBlock.gif                { 
InBlock.gif                        result.InnerHtml += g.Group + "个字符:<br />"
InBlock.gif 
InBlock.gif                        foreach (string s in g.Member) 
InBlock.gif                        { 
InBlock.gif                                result.InnerHtml += "--" + s + "<br />"
InBlock.gif                        } 
InBlock.gif                } 
InBlock.gif                result.InnerHtml += "<br />"
InBlock.gif 
InBlock.gif 
InBlock.gif                // 与上面的GroupBy查询操作符相对应的查询语法 
InBlock.gif                var list2 = from a in _ary 
InBlock.gif                                        group a by a.Length into g 
InBlock.gif                                        select new { Group = g.Key, Member = g }; 
InBlock.gif 
InBlock.gif                foreach (var g in list2) 
InBlock.gif                { 
InBlock.gif                        result.InnerHtml += g.Group + "个字符:<br />"
InBlock.gif 
InBlock.gif                        foreach (string s in g.Member) 
InBlock.gif                        { 
InBlock.gif                                result.InnerHtml += "--" + s + "<br />"
InBlock.gif                        } 
InBlock.gif                } 
InBlock.gif                result.InnerHtml += "<br />"
InBlock.gif        }
 
 
运行结果
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> 
InBlock.gif        /// Join - Join查询;延迟 
InBlock.gif        /// </summary> 
InBlock.gif        void Summary_Join() 
InBlock.gif        { 
InBlock.gif                // 使用Join查询操作符 
InBlock.gif                var products = _ctx.Products.Join( 
InBlock.gif                        _ctx.Categories,    
InBlock.gif                        p => p.CategoryID,    
InBlock.gif                        c => c.CategoryID,    
InBlock.gif                        (p, c) => new { c.CategoryName, p.ProductName }).Take(5); 
InBlock.gif 
InBlock.gif                foreach (var p in products) 
InBlock.gif                { 
InBlock.gif                        result.InnerHtml += p.CategoryName + " - " + p.ProductName + "<br />"
InBlock.gif                } 
InBlock.gif                result.InnerHtml += "<br />"
InBlock.gif 
InBlock.gif 
InBlock.gif                // 与上面的Join查询操作符相对应的查询语法 
InBlock.gif                var products2 = (from p in _ctx.Products 
InBlock.gif                                                 join c in _ctx.Categories 
InBlock.gif                                                 on p.CategoryID equals c.CategoryID 
InBlock.gif                                                 select new { c.CategoryName, p.ProductName }).Take(5); 
InBlock.gif 
InBlock.gif                foreach (var p in products2) 
InBlock.gif                { 
InBlock.gif                        result.InnerHtml += p.CategoryName + " - " + p.ProductName + "<br />"
InBlock.gif                } 
InBlock.gif                result.InnerHtml += "<br />"
InBlock.gif        }
 
运行结果
Beverages - Chai
Beverages - Chang
Condiments - Aniseed Syrup
Condiments - Chef Anton's Cajun Seasoning
Condiments - Chef Anton's Gumbo Mix

GroupJoin - 分组Join查询;延迟
InBlock.gif /// <summary> 
InBlock.gif        /// GroupJoin - 分组Join查询;延迟 
InBlock.gif        /// </summary> 
InBlock.gif        void Summary_GroupJoin() 
InBlock.gif        { 
InBlock.gif                // 使用GroupJoin查询操作符 
InBlock.gif                var products = _ctx.Categories.GroupJoin( 
InBlock.gif                        _ctx.Products,    
InBlock.gif                        c => c.CategoryID,    
InBlock.gif                        p => p.CategoryID,    
InBlock.gif                        (p, g) => new { p.CategoryName, ProductCount = g.Count() }); 
InBlock.gif 
InBlock.gif                foreach (var g in products) 
InBlock.gif                { 
InBlock.gif                        result.InnerHtml += g.CategoryName + ":" + g.ProductCount + "<br />"
InBlock.gif 
InBlock.gif                } 
InBlock.gif                result.InnerHtml += "<br />"
InBlock.gif 
InBlock.gif 
InBlock.gif                // 与上面的GroupJoin查询操作符相对应的查询语法 
InBlock.gif                var products2 = from c in _ctx.Categories 
InBlock.gif                                                join p in _ctx.Products on c.CategoryID equals p.CategoryID into g 
InBlock.gif                                                select new { CategoryName = c.CategoryName, ProductCount = g.Count() }; 
InBlock.gif 
InBlock.gif 
InBlock.gif                foreach (var g in products2) 
InBlock.gif                { 
InBlock.gif                        result.InnerHtml += g.CategoryName + ":" + g.ProductCount + "<br />"
InBlock.gif 
InBlock.gif                } 
InBlock.gif                result.InnerHtml += "<br />"
InBlock.gif        }
 
运行结果





     本文转自webabcd 51CTO博客,原文链接:http://blog.51cto.com/webabcd/344995,如需转载请自行联系原作者

版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。

相关文章
《Linux内核设计的艺术:图解Linux操作系统架构设计与实现原理》——2.10 初始化缓冲区管理结构
本节书摘来自华章计算机《Linux内核设计的艺术:图解Linux操作系统架构设计与实现原理》一书中的第2章,第2.10节,作者:新设计团队著, 更多章节内容可以访问云栖社区“华章计算机”公众号查看。
1142 0
EMR Spark Relational Cache 利用数据预组织加速查询
本文介绍了EMR Spark的Relational Cache如何从数据量较大的Cube中快速提取出所需数据加速查询的原理。通过列式存储、文件索引、Z-Order等技术,我们可以快速过滤数据,大大减少实际发生的IO数据量,避免IO瓶颈的出现,从而优化整体查询性能。
921 0
《Linux内核设计的艺术:图解Linux操作系统架构设计与实现原理》——2.11 初始化硬盘
本节书摘来自华章计算机《Linux内核设计的艺术:图解Linux操作系统架构设计与实现原理》一书中的第2章,第2.11节,作者:新设计团队著, 更多章节内容可以访问云栖社区“华章计算机”公众号查看。
818 0
《Linux内核设计的艺术:图解Linux操作系统架构设计与实现原理》——2.12 初始化软盘
本节书摘来自华章计算机《Linux内核设计的艺术:图解Linux操作系统架构设计与实现原理》一书中的第2章,第2.12节,作者:新设计团队著, 更多章节内容可以访问云栖社区“华章计算机”公众号查看。
800 0
21119
文章
0
问答
文章排行榜
最热
最新
相关电子书
更多
《2021云上架构与运维峰会演讲合集》
立即下载
《零基础CSS入门教程》
立即下载
《零基础HTML入门教程》
立即下载