亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

LINQ基礎(chǔ)之From和GroupBy子句

 更新時間:2022年04月20日 10:42:04   作者:農(nóng)碼一生  
這篇文章介紹了LINQ使用From和GroupBy子句的方法,文中通過示例代碼介紹的非常詳細。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下

FROM子句

一、簡單FROM子句

獲取數(shù)據(jù)源:

var queryAllCustomers =
        from cust in Customers
        select cust;

分析:

查詢表達式必須以from子句開頭
例子中cust是范圍變量,范圍變量類似于foreach循環(huán)中的迭代變量,但在查詢表達式中,實際上不發(fā)生迭代。執(zhí)行查詢時,范圍變量將用作對Customers中的每個后續(xù)元素的引用。因為編譯器可以推斷cust的類型,所以不必顯示指定此類型。
Customers是數(shù)據(jù)源,實現(xiàn)了IEnumerable或IEnumerable(T)或其派生接口的

二、復(fù)合FROM子句

查詢出成績有90分以上的學(xué)生,得到他們的名字和成績

//數(shù)據(jù)源
 IList<Student> students = new List<Student>
{
    new Student{ Name="Kevin", Score=new List<int>{89,93,88,78}},
    new Student{ Name="Jackie",Score=new List<int>{92,87,83,91}},
    new Student{ Name="Helen",Score=new List<int>{53,76,72,62}}
};
            //所有的Linq查詢操作都由以下三個不同的操作組成:
            //1.獲得數(shù)據(jù)源
            //2.創(chuàng)建查詢
            //3.執(zhí)行查詢

            //使用復(fù)合from子句查詢命令
            var getStudent =
                    from student in students
                    from score in student.Score
                    where score > 90
                    select new { Name = student.Name, Score = score };

            foreach (var st in getStudent)
            {
                Console.WriteLine("NAME:{0},SCORE:{1}",st.Name,st.Score);
            }

分析:

我們可以看到學(xué)生對象中有個Score屬性,Score屬性本身就是List集合,這時候我們就要用到復(fù)合from子句進行查詢了。首先遍歷學(xué)生對象集合中的每個學(xué)生對象,然后在用另一個from子句,對每個學(xué)生對象中的Score屬性進行遍歷,篩選出含有90分以上的學(xué)生信息進行返回。

GroupBy子句

一、簡介

group子句返回一個IGrouping(T Key,T element)對象序列,編譯時,group子句被轉(zhuǎn)換成對GroupBy方法的調(diào)用。

二、案例

案例一:

根據(jù)首字母分組,并打印到控制臺

//數(shù)據(jù)源
string[] fruits = { "apple", "banana", "peach", "orange", "melon", "lemon" };

//分組查詢的查詢語句
var query = from f in fruits
            group f by f[0];

//執(zhí)行查詢
foreach (var letters in query)
{
    Console.WriteLine("words that start with letter:" + letters.Key);
    foreach (var word in letters)
    {
        Console.WriteLine(word);
    }
}

分析:

首先遍歷字符串數(shù)組中的每個字符串,然后根據(jù)每個字符串的首字母進行分組,返回結(jié)果.

案例二:

var query = from f in fruits
             group f by f[0] into g
             where g.Key == 'p' || g.Key == 'b'
             select g;

分析:

如果您想要對每個組執(zhí)行附加查詢操作,則可以使用into上下文關(guān)鍵字指定一個臨時標識符。使用into時,必須繼續(xù)編寫該查詢,并最終用一個select語句或另一個group子句結(jié)束該查詢。

案例三:

string[] strs = { "胡廣成", "張國榮", "劉德華", "故國冰封", "萬里雪飄", "AAA", "胡翰軒", "張杰" };
            var list5 = strs
                //.Where(a => a.Length == 3)
                .Select(item => item)
                .GroupBy(item => item.Length);//分組   分組依據(jù)是字符串的長度
            foreach (var item in list5)
            {
                Console.WriteLine("----------------");
                Console.WriteLine($"分組字段是{item.Key}");  //item.Key是分組依據(jù) 顯示分組字段
                foreach (var items in item)//內(nèi)層循環(huán)遍歷分組項
                {
                    Console.WriteLine(items);
                }
            }

分析:

按照名字的長度進行分組顯示。

三、多字段分組示例

GroupBy(x => new { x.a , x.b, x.c }).Select( x=> ( new Class名 { a=x.Key.a , b=x.Key.b , c = x.Key.c } ))

到此這篇關(guān)于LINQ使用From和GroupBy子句的文章就介紹到這了。希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論