C#中Linq查詢基本操作使用實(shí)例
摘要:本文介紹Linq查詢基本操作(查詢關(guān)鍵字)
- from 子句
- where 子句
- select子句
- group 子句
- into 子句
- orderby 子句
- join 子句
- let 子句
- 復(fù)合from子句
- 在某些情況下,源序列中的每個(gè)元素本身可能是序列(集合),也可能包含序列
- 用語訪問單個(gè)數(shù)據(jù)庫中的內(nèi)部集合
- 使用多個(gè)from字句執(zhí)行連接
- 可以包含多個(gè)可從獨(dú)立數(shù)據(jù)源生成補(bǔ)充查詢的from字句
復(fù)合(顧名思義就是有多from的字句)實(shí)例:
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)建一個(gè)可以查詢自身的可枚舉類型
- 使查詢只能對范圍變量word調(diào)用一次ToLower。
如果不使用let,則必須在where字句的每個(gè)謂詞中調(diào)用ToLower
例:把每個(gè)單詞開頭包含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()//把每個(gè)字母小寫 where w[0] == 'a' || w[0] == 'e' select word; foreach (var s in query)
{
Console.WriteLine(s);
}
Console.ReadLine();
}
}
where 關(guān)鍵字 (篩選)
- 一個(gè)查詢表達(dá)式可以包含多個(gè)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字句可以使返回的序列(組)按升序或降序。
- 可以指定多個(gè)鍵,以便執(zhí)行一個(gè)或多個(gè)次要排序操作
- 默認(rèn)排序順序?yàn)樯?
- 編譯時(shí),orderby字句被轉(zhuǎn)換為對OrderBy方法的調(diào)用。orderby字句中的多個(gè)鍵被轉(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 字句返回一個(gè)IGrouping(TKey,Telement)對象序列
- 編譯時(shí),group子句被轉(zhuǎn)換為對GroupBy方法的調(diào)用
(LINQ查詢表達(dá)式可以以select或者Group結(jié)束) (如果想要對每個(gè)組執(zhí)行附加查詢操作,則可以使用into上下文關(guān)鍵字指定一個(gè)臨時(shí)標(biāo)識(shí)符,使用into時(shí),必須繼續(xù)編寫該查詢,并最終用一個(gè)select語句或另一 個(gè)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]”分組,本例中是用第一個(gè)字母分組
join 關(guān)鍵字 (聯(lián)接)
- 使用join 子句可以將來自不同源序列并且在對象模型中沒有直接關(guān)系的元素相關(guān)聯(lián)
- 唯一的要求是每個(gè)源中的元素需要共享某個(gè)可以進(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é)合起來,以指定要在某個(gè)左則元素不具有匹配元素時(shí)產(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 運(yùn)算符
- join 子句執(zhí)行同等聯(lián)接。換句話說,只能基于兩個(gè)鍵之間的相等關(guān)系進(jìn)行匹配
- 為了表明所有聯(lián)接都是相等聯(lián)接,join子句使用equals關(guān)鍵字而不是==運(yùn)算符
復(fù)合鍵
- 使用復(fù)合鍵可以測試多個(gè)值是否相等
select 關(guān)鍵字 選擇
- C#集合查詢Linq在項(xiàng)目中使用詳解
- C# Linq延遲查詢的執(zhí)行實(shí)例代碼
- c# Linq查詢詳解
- c# 動(dòng)態(tài)構(gòu)建LINQ查詢表達(dá)式
- C#使用LINQ查詢表達(dá)式的基本子句總結(jié)
- C# linq查詢之動(dòng)態(tài)OrderBy用法實(shí)例
- C#中Linq延遲查詢的例子
- C#使用linq語句查詢數(shù)組中以特定字符開頭元素的方法
- C#使用linq查詢大數(shù)據(jù)集的方法
- C# LINQ查詢表達(dá)式及對應(yīng)LAMBDA表達(dá)式的用法
- C#使用LINQ查詢操作符實(shí)例代碼(一)
相關(guān)文章
C#開發(fā)教程之利用特性自定義數(shù)據(jù)導(dǎo)出到Excel
這篇文章主要介紹了C#開發(fā)教程之利用特性自定義數(shù)據(jù)導(dǎo)出到Excel的相關(guān)資料,需要的朋友可以參考下2016-07-07
在Unity中實(shí)現(xiàn)動(dòng)畫的正反播放代碼
這篇文章主要介紹了在Unity中實(shí)現(xiàn)動(dòng)畫的正反播放代碼,非常的實(shí)用,這里推薦給大家,希望大家能夠喜歡。2015-03-03
使用C#發(fā)送Http請求實(shí)現(xiàn)模擬登陸實(shí)例
本文主要介紹了使用C#發(fā)送Http請求實(shí)現(xiàn)模擬登陸實(shí)例,模擬登陸的原理簡單,想要了解的朋友可以了解一下。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#實(shí)現(xiàn)ComboBox控件顯示出多個(gè)數(shù)據(jù)源屬性的方法
這篇文章主要介紹了C#實(shí)現(xiàn)ComboBox控件顯示出多個(gè)數(shù)據(jù)源屬性的方法,實(shí)例分析了ComboBox控件的使用技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-09-09
C#開發(fā)Winform實(shí)現(xiàn)文件操作案例
這篇文章介紹了C#開發(fā)Winform實(shí)現(xiàn)文件操作的案例,文中通過示例代碼介紹的非常詳細(xì)。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-04-04

