欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

LINQ(語言集成查詢)使用案例

 更新時(shí)間:2022年04月25日 09:30:20   作者:農(nóng)碼一生  
這篇文章介紹了LINQ(語言集成查詢)的使用案例,文中通過示例代碼介紹的非常詳細(xì)。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下

概念

語言集成查詢 (LINQ) 是一系列直接將查詢功能集成到 C# 語言的技術(shù)統(tǒng)稱。

數(shù)據(jù)查詢歷來都表示為簡單的字符串,沒有編譯時(shí)類型檢查或 IntelliSense 支持。 此外,需要針對每種類型的數(shù)據(jù)源了解不同的查詢語言:SQL 數(shù)據(jù)庫、XML 文檔、各種 Web 服務(wù)等。
借助 LINQ,查詢成為了最高級(jí)的語言構(gòu)造,就像類、方法和事件一樣。 可以使用語言關(guān)鍵字和熟悉的運(yùn)算符針對強(qiáng)類型化對象集合編寫查詢。 LINQ 系列技術(shù)提供了針對對象 (LINQ to Objects)、關(guān)系數(shù)據(jù)庫 (LINQ to SQL) 和 XML (LINQ to XML) 的一致查詢體驗(yàn)。

對于編寫查詢的開發(fā)者來說,LINQ 最明顯的“語言集成”部分就是查詢表達(dá)式。
查詢表達(dá)式采用聲明性查詢語法編寫而成。 使用查詢語法,可以用最少的代碼對數(shù)據(jù)源執(zhí)行篩選、排序和分組操作。 可使用相同的基本查詢表達(dá)式模式來查詢和轉(zhuǎn)換 SQL 數(shù)據(jù)庫、ADO .NET 數(shù)據(jù)集、XML 文檔和流以及 .NET 集合中的數(shù)據(jù)。

在 C# 中可為以下對象編寫 LINQ 查詢:SQL Server 數(shù)據(jù)庫、XML 文檔、ADO.NET 數(shù)據(jù)集以及支持 IEnumerable 或泛型 IEnumerable 接口的任何對象集合。 此外,第三方也為許多 Web 服務(wù)和其他數(shù)據(jù)庫實(shí)現(xiàn)提供了 LINQ 支持。

實(shí)現(xiàn)案例

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LinkConsole
{
    class Program
    {
        static void Main(string[] args)
        {
              //-------------最基本的簡單查詢----------------//
            List<int> numbers = new List<int>() { 1,2,3,4,5,6,7,8,9,10};
            var numQuery = from num in numbers
                          where num % 2 == 0
                           select num;
            foreach (var num in numQuery)
            {
               Console.WriteLine("{0,1}", num);
            }

             //-------------讀取List<>中的句子----------------//
             FormExpDemo2();
            //-------------復(fù)合form子句----------------//
            FormExpDemo();
            //-------------多個(gè)from句子---------------//
            FormExpDemo3();
            //-------------where-------------------//
             WhereExpDemo();
            //-------------select------------------//
            SelectDemo();
            //-------------group--------------------//
            GroupDemo();
            //-------------into------------------------//
            IntoDemo();
            //--------------OrderBy--------------------//
            ThenByDemo();
            //--------------let----------------------//
            LetDemo();
            //--------------join--------------------//
            JoinDemo();
            Console.ReadLine();
        }
        public class CustomerInfo
        {
            public string Name { get; set; }
            public int Age { get; set; }
            public string Tel { get; set; }
            public List<string> telTable { get; set; }
        }
        public static void FormExpDemo2()
        {
            //
            List<CustomerInfo> customers = new List<CustomerInfo>
            {
                new CustomerInfo{ Name = "歐陽曉曉",Age = 35,Tel = "123"},
                new CustomerInfo{ Name = "上官飄飄",Age = 17,Tel = "456"},
                new CustomerInfo{ Name = "諸葛菲菲",Age = 23,Tel = "789"}
            };
            var query = from ci in customers
                        where ci.Age > 20
                        select ci;
            foreach (var ci in query)
            {
                Console.WriteLine("姓名:{0}年齡:{1}電話:{2}", ci.Name, ci.Age, ci.Tel);
            }
        }

        //復(fù)合from子句    // 相當(dāng)于兩個(gè)for循環(huán)而已
        private static void FormExpDemo()
        {
            List<CustomerInfo> customers = new List<CustomerInfo>
            {
                new CustomerInfo { Name = "歐陽小小",Age= 35,telTable = new List<string> {"123","234"} },
                new CustomerInfo { Name = "上官飄飄",Age= 35,telTable = new List<string> {"456","567"} },
                new CustomerInfo { Name = "諸葛菲菲",Age= 35,telTable = new List<string> {"789","456"} },
            };
            //查詢包含電話號(hào)碼456的客戶
            var query = from ci in customers
                        from tel in ci.telTable
                        where tel.IndexOf("456") > -1
                        select ci;
            foreach (var ci in query)
            {
                Console.WriteLine("姓名:{0}年齡:{1}", ci.Name, ci.Age);
                foreach (var tel in ci.telTable)
                {
                    Console.WriteLine("          電話:{0}", tel);
                }

            }
        }

        //多個(gè)from子句,和復(fù)合子句看起來是一樣的,其實(shí)不一樣,一個(gè)是單個(gè)數(shù)據(jù)源中的子元素的集合,一個(gè)是對多個(gè)數(shù)據(jù)源進(jìn)行查詢
        private static void FormExpDemo3()
        {
            List<CustomerInfo> customers = new List<CustomerInfo>
            {
                new CustomerInfo{ Name = "歐陽曉曉",Age = 35,Tel = "123"},
                new CustomerInfo{ Name = "上官飄飄",Age = 77,Tel = "456"},
                new CustomerInfo{ Name = "諸葛菲菲",Age = 23,Tel = "789"}
            };
            List<CustomerInfo> customers2 = new List<CustomerInfo>
            {
                new CustomerInfo{ Name = "令狐沖",Age = 25,Tel = "123"},
                new CustomerInfo{ Name = "東方不敗",Age = 15,Tel = "456"},
                new CustomerInfo{ Name = "任盈盈",Age = 13,Tel = "789"}
            };
            //在customers 中尋找年齡大于20的客戶
            //在customenrs中尋找年齡小于30歲的客戶
            var query = from custo in customers
                        where custo.Age > 20
                        from custo2 in customers2
                        where custo2.Age < 30
                        select new { custo, custo2 };
            foreach (var ci in query)
            {
                Console.WriteLine("{0},{1}", ci.custo.Name, ci.custo2.Name);//這樣得到的是一個(gè)交叉聯(lián)結(jié)表,有點(diǎn)類似于SQL中的笛卡爾沉積
            }
        }

        //where子句查詢
        //where就是用來篩選元素的,除了開始和結(jié)束位置,where可以在任意位置使用,
        //一個(gè)LIKQ語句中可以有where子句,也可以沒有,可以有一個(gè),也可以有多個(gè)。
        //多個(gè)where子句之間的關(guān)系相當(dāng)于邏輯“與”,每個(gè)子句中又可以包含多個(gè)用“謂詞”鏈接的邏輯表達(dá)式,&&,或者||
        private static void WhereExpDemo()
        {
            List<CustomerInfo> clist = new List<CustomerInfo>
            {
                 new CustomerInfo{ Name="歐陽曉曉", Age=35, Tel ="1330708****"},
                 new CustomerInfo{ Name="上官飄飄", Age=17, Tel ="1592842****"},
                 new CustomerInfo{ Name="令狐沖", Age=23, Tel ="1380524****"}
            };
            //可以查詢符合多個(gè)條件的人(名字是三個(gè)字或者姓令的,但年齡必須大于20)
            var query = from custo in clist
                        where (custo.Name.Length == 3 || custo.Name.Substring(0, 1) == "令") && custo.Age > 20
                        select custo;//select 也可以改成,比如custo.Name?;蛘哂靡粋€(gè)函數(shù),把變量傳出去
            foreach (var ci in query)
            {
                Console.WriteLine("姓名:{0}年齡:{1}電話:{2}", ci.Name, ci.Age, ci.Tel);

            }

            //where中使用自定義函數(shù),查詢?nèi)齻€(gè)字并且姓令的客戶
            var query2 = from custo in clist
                         where (custo.Name.Length == 3 && ChechName(custo.Name))
                         select custo;
            foreach (var ci in query2)
            {
                Console.WriteLine("姓名:{0}年齡:{1}電話:{2}", ci.Name, ci.Age, ci.Tel);
            }


        }
        private static bool ChechName(string name)
        {
            if (name.Substring(0, 1) == "令")
                return true;
            else
                return false;
        }

        //select 用法舉例
        private static void SelectDemo()
        {
            List<CustomerInfo> clist = new List<CustomerInfo>
            {
                 new CustomerInfo{ Name="歐陽曉曉", Age=35, Tel ="1330708****"},
                 new CustomerInfo{ Name="上官飄飄", Age=17, Tel ="1592842****"},
                 new CustomerInfo{ Name="令狐沖", Age=23, Tel ="1380524****"}
            };
            string[] names = { "令狐沖", "任盈盈", "楊過", "小龍女", "歐陽小夏", "歐陽曉曉" };
            //查詢在給定謂詞數(shù)組里存在的客戶
            var query = from custo in clist
                        where custo.Age < 30
                        select new MyCustomerInfo { Name = custo.Name, Tel = custo.Tel };
            foreach (var ci in query)
            {
                Console.WriteLine("姓名:{0}電話:{1}類型{2}", ci.Name, ci.Tel, ci.GetType().FullName);
            }
        }
        public class MyCustomerInfo
        {
            public string Name { get; set; }
            public string Tel { get; set; }
        }

        //-------------------Group----------------------//
        static List<CustomerInfo> clist = new List<CustomerInfo>
        {
             new CustomerInfo{ Name="歐陽曉曉", Age=35, Tel ="1330708"},
             new CustomerInfo{ Name="上官飄飄", Age=17, Tel ="1592842"},
             new CustomerInfo{ Name="歐陽錦鵬", Age=35, Tel ="1330708"},
             new CustomerInfo{ Name="上官無忌", Age=23, Tel ="1380524"}
        };
        private static void GroupDemo()
        {
            //按照名字的前兩個(gè)字進(jìn)行分組
            var query = from custo in clist
                        group custo by custo.Name.Substring(0, 2);
            foreach (IGrouping<string, CustomerInfo> group in query)
            {
                Console.WriteLine("分組鍵:{0}", group.Key);
                foreach (var ci in group)
                {
                    Console.WriteLine("姓名:{0}電話:{1}", ci.Name, ci.Tel);
                }
                Console.WriteLine("*********************");
            }
            //可以知道group子句返回的是一個(gè)IGrouping<TKey,TElement>泛型接口的對象集合
            //TKey是鍵的對象類型,在用于group子句的時(shí)候,編譯器會(huì)識(shí)別數(shù)據(jù)類型,用于存儲(chǔ)分組的鍵值,也就是根據(jù)什么分的組
            //TElement是指的對象類型用于分配儲(chǔ)存結(jié)果,變量基于這個(gè)接口的類型就是遍歷這個(gè)值,也就是分組的對象
        }

        //----------------into子句---------------//
        private static void IntoDemo()
        {
            //into提供了一個(gè)臨時(shí)標(biāo)識(shí)符,它儲(chǔ)存了into子句前面的查詢內(nèi)容,使他后面的子句可以方便使用,再次查詢投影
            var query = from custo in clist
                        group custo by custo.Name.Substring(0, 2) into gpcustomer
                        orderby gpcustomer.Key descending //排序,
                        select gpcustomer;
            Console.WriteLine("into用于group子句");
            foreach (var group in query)
            {
                Console.WriteLine("分組見:{0}", group.Key);
                foreach (var ci in group)
                {
                    Console.WriteLine("姓名:{0}電話:{1}", ci.Name, ci.Tel);
                }
                Console.WriteLine("***********************");
            }
            var query2 = from custo in clist
                         select new { NewName = custo.Name, NewAge = custo.Age } into newCustomer
                         orderby newCustomer.NewAge
                         select newCustomer;
            Console.WriteLine("into用于select子句");
            foreach (var ci in query2)
            {
                Console.WriteLine("{0}年齡:{1}", ci.NewName, ci.NewAge);
            }
        }

        //---------------排序子句--------------------//
        //LINQ可以按元素的一個(gè)或者多個(gè)屬性對元素進(jìn)行排序,表達(dá)式的排序方式分為OrderBy、OrderByDescending、ThenBy、ThenByDescending
        //加了Descending的就是降序,沒有加的就是升序

        private static void ThenByDemo()
        {
            List<CustomerInfo> clist = new List<CustomerInfo>
            {
                 new CustomerInfo{ Name="歐陽曉曉 ", Age=35, Tel ="1330708****"},
                 new CustomerInfo{ Name="上官飄飄 ", Age=17, Tel ="1592842****"},
                 new CustomerInfo{ Name="郭靖 ", Age=17, Tel ="1330708****"},
                 new CustomerInfo{ Name="黃蓉 ", Age=17, Tel ="1300524****"}
            };
            //按照年齡升序,再按照名字的字?jǐn)?shù)次要排序
            var query = from customer in clist
                        orderby customer.Age, customer.Name.Length
                        select customer;
            Console.WriteLine("按年齡排列,按名字字?jǐn)?shù)進(jìn)行次要排序");
            foreach (var ci in query)
            {
                Console.WriteLine("姓名:{0} 年齡:{1} 電話:{2}",ci.Name, ci.Age, ci.Tel);

            }
            //按年齡降序,再按名字的字?jǐn)?shù)降序次要排列
            var query2 = from customer in clist
                         orderby customer.Age descending , customer.Name.Length descending
                         select customer;
            Console.WriteLine("\n按年齡排列,按名字字?jǐn)?shù)進(jìn)行降序次要排列");
            foreach (var ci in query2)
            {
                Console.WriteLine("姓名:{0} 年齡:{1} 電話:{2}", ci.Name, ci.Age, ci.Tel);
            }

        }

        //--------------let子句---------------------//

        private static void LetDemo()
        {
            var query = from custo in clist
                        let g = custo.Name.Substring(0, 1)//let建立一個(gè)范圍變量,在where中使用
                        where g == "歐" || g == "上"http://也可以不寫,寫成customer.Name.Substring(0, 1) == "郭" || customer.Name.Substring(0, 1) == "黃"
                        select custo;
            foreach (var ci in query)
            {
                Console.WriteLine("姓名:{0} 年齡:{1} 電話:{2}", ci.Name, ci.Age, ci.Tel);
            }
        }

        //-------------join子句-------------------//
        private static void JoinDemo()
        {
            //如果兩個(gè)數(shù)據(jù)源中的屬性可以進(jìn)行相等比較,那么兩個(gè)句子可以用join進(jìn)行關(guān)聯(lián),比較的符號(hào)為equal,而不是==
            List<CustomerInfo> clist = new List<CustomerInfo>
            {
               new CustomerInfo{ Name="歐陽曉曉", Age=35, Tel ="1330708****"},
               new CustomerInfo{ Name="上官飄飄", Age=17, Tel ="1592842****"},
               new CustomerInfo{ Name="郭靖", Age=17, Tel ="1330708****"},
               new CustomerInfo{ Name="黃蓉", Age=17, Tel ="1300524****"}
            };
            List<CustomerTitle> titleList = new List<CustomerTitle>
            {
               new CustomerTitle{ Name="歐陽曉曉", Title="歌手"},
               new CustomerTitle{ Name="郭靖", Title="大俠"},
               new CustomerTitle{ Name="郭靖", Title="洪七公徒弟"},
               new CustomerTitle{ Name="黃蓉", Title="才女"},
               new CustomerTitle{ Name="黃蓉", Title="丐幫幫主"}
            };
            //根據(jù)姓名進(jìn)行內(nèi)部聯(lián)結(jié)
            var query = from customer in clist
                        join title in titleList
                        on customer.Name equals title.Name
                        select new { Name = customer.Name, Age = customer.Age, Title = title.Title };
            foreach (var ci in query)
            {
                Console.WriteLine("姓名:{0} 年齡:{1}{2}", ci.Name, ci.Age, ci.Title);
            }
            //根據(jù)姓名進(jìn)行分組聯(lián)結(jié)
            Console.WriteLine("\n根據(jù)姓名進(jìn)行分組聯(lián)結(jié)");
            var query2 = from customer in clist
                         join title in titleList
                         on customer.Name equals title.Name into tgroup
                         select new { Name = customer.Name, Titles = tgroup };
            foreach (var g in query2)
            {
                Console.WriteLine(g.Name);
                foreach (var g2 in g.Titles)
                {
                    Console.WriteLine("     {0}", g2.Title);
                }
            }
            //根據(jù)姓名進(jìn)行 左外部聯(lián)結(jié)
            Console.WriteLine("\n左外部聯(lián)結(jié)");
            var query3 = from customer in clist
                         join title in titleList
                         on customer.Name equals title.Name into tgroup
                         from subTitle in tgroup.DefaultIfEmpty()
                         select new { Name = customer.Name, Title = (subTitle == null ? "空缺" : subTitle.Title) };
            foreach (var ci in query3)
            {
                Console.WriteLine("姓名:{0} ", ci.Name, ci.Title);
            }
        }
        public class CustomerTitle
        {
            public string Name { get; set; }
            public string Title { get; set; }
        }
    }
}

到此這篇關(guān)于LINQ(語言集成查詢)使用案例的文章就介紹到這了。希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • c#?如何將字符串轉(zhuǎn)換為大寫或小寫

    c#?如何將字符串轉(zhuǎn)換為大寫或小寫

    這篇文章主要介紹了c#?如何將字符串轉(zhuǎn)換為大寫或小寫,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-06-06
  • C#中派生類調(diào)用基類構(gòu)造函數(shù)用法分析

    C#中派生類調(diào)用基類構(gòu)造函數(shù)用法分析

    這篇文章主要介紹了C#中派生類調(diào)用基類構(gòu)造函數(shù)用法,實(shí)例分析了派生類調(diào)用基類構(gòu)造函數(shù)的技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下
    2015-04-04
  • C#單例類的實(shí)現(xiàn)方法

    C#單例類的實(shí)現(xiàn)方法

    這篇文章主要介紹了C#單例類的實(shí)現(xiàn)方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-06-06
  • C#中字符串編碼處理

    C#中字符串編碼處理

    C#中字符串編碼處理,需要的朋友可以參考一下
    2013-03-03
  • C#實(shí)現(xiàn)歸并排序

    C#實(shí)現(xiàn)歸并排序

    這篇文章介紹了C#實(shí)現(xiàn)歸并排序的方法,文中通過示例代碼介紹的非常詳細(xì)。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-04-04
  • C# 函數(shù)覆蓋總結(jié)學(xué)習(xí)(推薦)

    C# 函數(shù)覆蓋總結(jié)學(xué)習(xí)(推薦)

    下面小編就為大家?guī)硪黄狢# 函數(shù)覆蓋總結(jié)學(xué)習(xí)(推薦)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2016-05-05
  • C#開發(fā)WinForm之DataGridView開發(fā)詳解

    C#開發(fā)WinForm之DataGridView開發(fā)詳解

    這篇文章主要介紹了C#開發(fā)WinForm之DataGridView開發(fā)詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-01-01
  • C#字典Dictionary的用法說明(注重性能版)

    C#字典Dictionary的用法說明(注重性能版)

    這篇文章主要介紹了C#字典Dictionary的用法說明,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2021-04-04
  • C#使用BitConverter與BitArray類進(jìn)行預(yù)定義基礎(chǔ)類型轉(zhuǎn)換

    C#使用BitConverter與BitArray類進(jìn)行預(yù)定義基礎(chǔ)類型轉(zhuǎn)換

    這篇文章介紹了C#使用BitConverter與BitArray類進(jìn)行預(yù)定義基礎(chǔ)類型轉(zhuǎn)換的方法,文中通過示例代碼介紹的非常詳細(xì)。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-05-05
  • C#實(shí)現(xiàn)簡易點(diǎn)餐功能

    C#實(shí)現(xiàn)簡易點(diǎn)餐功能

    這篇文章主要為大家詳細(xì)介紹了C#實(shí)現(xiàn)簡易點(diǎn)餐功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-07-07

最新評論