天天看點

步步為營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及其對應的查詢文法

介紹

    ·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) 

        }

運作結果

類别名稱:Beverages

類别名稱:Condiments

類别名稱:Confections

類别名稱:Dairy Products

類别名稱:Grains/Cereals

類别名稱:Meat/Poultry

類别名稱:Produce

類别名稱:Seafood

Where - Where查詢;延遲

        /// Where - Where查詢;延遲 

        void Summary_Where() 

                // 使用Where查詢操作符 

                var ary = _ary.Where(a => a.StartsWith("w") && a.EndsWith("f")); 

                foreach (string s in ary) 

                        result.InnerHtml += s + "<br />"; 

                // 與上面的Where查詢操作符相對應的查詢文法 

                var ary2 = from a in _ary 

                                     where a.StartsWith("w") && a.EndsWith("f") 

                                     select a; 

                foreach (string s in ary2) 

wcf

wpf

wf

OrderBy - 按指定表達式對集合正序排序;延遲

OrderByDescending - 按指定表達式對集合倒序排序;延遲

 /// <summary> 

        /// OrderBy - 按指定表達式對集合正序排序;延遲 

        /// OrderByDescending - 按指定表達式對集合倒序排序;延遲 

        void Summary_OrderBy_OrderByDescending() 

                // 使用OrderBy查詢操作符 

                var ary = (from a in _ary 

                                     select a).OrderBy(a => a.Length); // OrderByDescending與OrderBy用法相同 

                // 與上面的OrderBy查詢操作符相對應的查詢文法 

                                     orderby a.Length ascending // orderby xxx descending與orderby xxx ascending用法相同 

css

linq

ssis

ssas

ssrs

xhtml

csharp

asp.net

sqlserver

javascript

silverlight

asp.net ajax

GroupBy - 分組;延遲

        /// GroupBy - 分組;延遲 

        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 />"; 

                        } 

                // 與上面的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) 

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查詢;延遲

        /// Join - Join查詢;延遲 

        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 />"; 

                // 與上面的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) 

Beverages - Chai

Beverages - Chang

Condiments - Aniseed Syrup

Condiments - Chef Anton's Cajun Seasoning

Condiments - Chef Anton's Gumbo Mix

GroupJoin - 分組Join查詢;延遲

        /// GroupJoin - 分組Join查詢;延遲 

        void Summary_GroupJoin() 

                // 使用GroupJoin查詢操作符 

                var products = _ctx.Categories.GroupJoin( 

                        _ctx.Products,    

                        (p, g) => new { p.CategoryName, ProductCount = g.Count() }); 

                foreach (var g in products) 

                        result.InnerHtml += g.CategoryName + ":" + g.ProductCount + "<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) 

     本文轉自webabcd 51CTO部落格,原文連結:http://blog.51cto.com/webabcd/344995,如需轉載請自行聯系原作者

繼續閱讀