C#中Linq查詢基本操作使用實例
摘要:本文介紹Linq查詢基本操作(查詢關(guān)鍵字)
- from 子句
- where 子句
- select子句
- group 子句
- into 子句
- orderby 子句
- join 子句
- let 子句
- 復(fù)合from子句
- 在某些情況下,源序列中的每個元素本身可能是序列(集合),也可能包含序列
- 用語訪問單個數(shù)據(jù)庫中的內(nèi)部集合
- 使用多個from字句執(zhí)行連接
- 可以包含多個可從獨立數(shù)據(jù)源生成補充查詢的from字句
復(fù)合(顧名思義就是有多from的字句)實例:
class Program
{ static void Main(string[] args)
{
List<Student> students = new List<Student> { new Student
{
LastName="xiaogui",Scores=new List<int>{97,42,91,60}}, new Student
{
LastName="xiaozhan",Scores=new List<int>{50,92,81,60}}, new Student
{
LastName="xiaolan",Scores=new List<int>{32,32,81,90}}, new Student
{
LastName="xiaowan",Scores=new List<int>{92,22,81,60}},
}; var query = from stuent in students from score in stuent.Scores where score > 90 select new {
Last = stuent.LastName,
score
}; foreach (var student in query)//大于90分的顯示出來 {
Console.WriteLine("{0} Score:{1}", student.Last, student.score);
}
Console.ReadLine();
}
}
public class Student
{ public string LastName { get; set; } public List<int> Scores { get; set; }
} public class Employee
{ public string First { get; set; } public string Last { get; set; } public int ID { get; set; }
}
執(zhí)行結(jié)果: xiaogui Score:97
xiaogui Score:91
xiaozhan Score:92
xiaowan Score:92
let 關(guān)鍵字(使用let字句擴(kuò)展范圍變量)
- 創(chuàng)建一個可以查詢自身的可枚舉類型
- 使查詢只能對范圍變量word調(diào)用一次ToLower。
如果不使用let,則必須在where字句的每個謂詞中調(diào)用ToLower
例:把每個單詞開頭包含a或者e的找出來
using System; using System.Linq; public class Test
{ static void Main(string[] args)
{ string[] strings = { "A penny saved is a penny earned.", "The aaxly sdj", "the pa is no" }; var query = from sentence in strings
let words = sentence.Split(' ')//用空格分割成數(shù)組 from word in words
let w = word.ToLower()//把每個字母小寫 where w[0] == 'a' || w[0] == 'e' select word; foreach (var s in query)
{
Console.WriteLine(s);
}
Console.ReadLine();
}
}
where 關(guān)鍵字 (篩選)
- 一個查詢表達(dá)式可以包含多個where字句
例:(把包含a的找出來)
using System; using System.Linq; public class Test
{ static void Main(string[] args)
{ string[] str = { "a", "b", "c" }; var query = from s in str where s == "a" select s; foreach (var s in query)
{
Console.WriteLine(s);
}
Console.ReadLine();
}
}
orderby 關(guān)鍵字 (排序)
- 在查詢表達(dá)式中,orderby字句可以使返回的序列(組)按升序或降序。
- 可以指定多個鍵,以便執(zhí)行一個或多個次要排序操作
- 默認(rèn)排序順序為升序
- 編譯時,orderby字句被轉(zhuǎn)換為對OrderBy方法的調(diào)用。orderby字句中的多個鍵被轉(zhuǎn)換為ThenBy方法調(diào)用
descending 降序
ascending 升序
例1:升序
using System; using System.Linq; public class Test
{ static void Main(string[] args)
{ string[] str = { "a", "b", "c" }; var query = from s in str orderby s ascending select s;
}
}
結(jié)果為: a b c
例2:降序
using System; using System.Linq; public class Test
{ static void Main(string[] args)
{ string[] str = { "a", "b", "c" }; var query = from s in str orderby s descending select s;
}
}
結(jié)果為: c b a
group 關(guān)鍵字 (分組)
- group 字句返回一個IGrouping(TKey,Telement)對象序列
- 編譯時,group子句被轉(zhuǎn)換為對GroupBy方法的調(diào)用
(LINQ查詢表達(dá)式可以以select或者Group結(jié)束) (如果想要對每個組執(zhí)行附加查詢操作,則可以使用into上下文關(guān)鍵字指定一個臨時標(biāo)識符,使用into時,必須繼續(xù)編寫該查詢,并最終用一個select語句或另一 個group子句結(jié)束該查詢)
例:
using System; using System.Linq; public class Test
{ static void Main(string[] args)
{ string[] str = { "aa", "bb", "cc", "dd" }; var query = from s in str
group s by s[0]
into p where p.Key == 'a' || p.Key == 'b' || p.Key == 'c' orderby p.Key descending select p.Key; foreach (var s in query)
{
Console.WriteLine(s);
}
Console.ReadLine();
}
}
結(jié)果為: c b a
說明: group s by s[0] into p 意思是針對范圍變量s用“s[0]”分組,本例中是用第一個字母分組
join 關(guān)鍵字 (聯(lián)接)
- 使用join 子句可以將來自不同源序列并且在對象模型中沒有直接關(guān)系的元素相關(guān)聯(lián)
- 唯一的要求是每個源中的元素需要共享某個可以進(jìn)行比較以判斷是否相等的值
- join字句使用特殊的equals關(guān)鍵字比較指定的鍵是否相等
三中常見的連接類型
- 內(nèi)部聯(lián)接
- 分組聯(lián)接
- 左外部聯(lián)接
1.內(nèi)部聯(lián)接
var query = from a in str
join b in str2 on a.id equals b.id select new { Aname = a.name, Bname = b.name };
2.分組聯(lián)接:(into 可以將join暫存起來)
var query = from a in str
join b in str2 on a.id equals b.id
into c select new {Aname=a.name,Cs=c}
3.左外部聯(lián)接
- 在左外部聯(lián)接中,將返回左則源序列中所有元素,即使它們在右則序列中沒有匹配的元素也是如此。
- 若要在LINQ中執(zhí)行左外部聯(lián)接,請將DefaultifEmpty方法與分組聯(lián)接結(jié)合起來,以指定要在某個左則元素不具有匹配元素時產(chǎn)生的默認(rèn)右則元素??梢允褂胣ull作為任何引
用類型的默認(rèn)值,也可以指定用戶定義的默認(rèn)類型。
var query = from category in categories
join prod in products on category.id equals prod.CategoryID
into prodGroup from item prodGroup.defaultifEmpty( new Product{Name=string.empty,CategoryID=0}) select new {CatName=category.Name,ProdName=item.Name}
equalse 運算符
- join 子句執(zhí)行同等聯(lián)接。換句話說,只能基于兩個鍵之間的相等關(guān)系進(jìn)行匹配
- 為了表明所有聯(lián)接都是相等聯(lián)接,join子句使用equals關(guān)鍵字而不是==運算符
復(fù)合鍵
- 使用復(fù)合鍵可以測試多個值是否相等
select 關(guān)鍵字 選擇
相關(guān)文章
C#開發(fā)教程之利用特性自定義數(shù)據(jù)導(dǎo)出到Excel
這篇文章主要介紹了C#開發(fā)教程之利用特性自定義數(shù)據(jù)導(dǎo)出到Excel的相關(guān)資料,需要的朋友可以參考下2016-07-07
使用C#發(fā)送Http請求實現(xiàn)模擬登陸實例
本文主要介紹了使用C#發(fā)送Http請求實現(xiàn)模擬登陸實例,模擬登陸的原理簡單,想要了解的朋友可以了解一下。2016-10-10
C#中Dictionary與List的用法區(qū)別以及聯(lián)系詳解
List和Dictionary想必是我們平常用到最多的C#容器了,他們使用起來都很簡單,這篇文章主要給大家介紹了關(guān)于C#中Dictionary與List的用法區(qū)別以及聯(lián)系的相關(guān)資料,需要的朋友可以參考下2023-11-11
C#實現(xiàn)ComboBox控件顯示出多個數(shù)據(jù)源屬性的方法
這篇文章主要介紹了C#實現(xiàn)ComboBox控件顯示出多個數(shù)據(jù)源屬性的方法,實例分析了ComboBox控件的使用技巧,具有一定參考借鑒價值,需要的朋友可以參考下2015-09-09

