| 網(wǎng)站首頁(yè) | 關(guān)于我們 | 開發(fā)優(yōu)勢(shì) | 產(chǎn)品展示 |
| 合作企業(yè) | 新聞動(dòng)態(tài) | 聯(lián)系我們 | 電話聯(lián)系 |
文章作者:濟(jì)南軟件開發(fā) 時(shí)間:2016年12月20日
本文山東軟件公司介紹Linq查詢基本操作(查詢關(guān)鍵字)
- from 子句
- where 子句
- select子句
- group 子句
- into 子句
- orderby 子句
- join 子句
- let 子句
- 復(fù)合from子句
- 在某些情況下,源序列中的每個(gè)元素本身可能是序列(集合),也可能包含序列
- 用語(yǔ)訪問(wèn)單個(gè)數(shù)據(jù)庫(kù)中的內(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分的顯示出來(lái)
{
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è)可以查詢自身的可枚舉類型
- 使查詢只能對(duì)范圍變量word調(diào)用一次ToLower。
如果不使用let,則必須在where字句的每個(gè)謂詞中調(diào)用ToLower
例:把每個(gè)單詞開頭包含a或者e的找出來(lái)
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的找出來(lái))
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)樯?/p>
- 編譯時(shí),orderby字句被轉(zhuǎn)換為對(duì)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)對(duì)象序列
- 編譯時(shí),group子句被轉(zhuǎn)換為對(duì)GroupBy方法的調(diào)用
(LINQ查詢表達(dá)式可以以select或者Group結(jié)束) (如果想要對(duì)每個(gè)組執(zhí)行附加查詢操作,則可以使用into上下文關(guān)鍵字指定一個(gè)臨時(shí)標(biāo)識(shí)符,使用into時(shí),必須繼續(xù)編寫該查詢,并最終用一個(gè)select語(yǔ)句或另一 個(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
說(shuō)明: group s by s[0] into p 意思是針對(duì)范圍變量s用“s[0]”分組,本例中是用第一個(gè)字母分組
join 關(guān)鍵字 (聯(lián)接)
- 使用join 子句可以將來(lái)自不同源序列并且在對(duì)象模型中沒(méi)有直接關(guān)系的元素相關(guān)聯(lián)
- 唯一的要求是每個(gè)源中的元素需要共享某個(gè)可以進(jìn)行比較以判斷是否相等的值
- join字句使用特殊的equals關(guān)鍵字比較指定的鍵是否相等
三中常見(jià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暫存起來(lái))
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)接中,將返回左則源序列中所有元素,即使它們?cè)谟覄t序列中沒(méi)有匹配的元素也是如此。
- 若要在LINQ中執(zhí)行左外部聯(lián)接,請(qǐng)將DefaultifEmpty方法與分組聯(lián)接結(jié)合起來(lái),以指定要在某個(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)接。換句話說(shuō),只能基于兩個(gè)鍵之間的相等關(guān)系進(jìn)行匹配
- 為了表明所有聯(lián)接都是相等聯(lián)接,join子句使用equals關(guān)鍵字而不是==運(yùn)算符
想要了解更多詳情歡迎來(lái)電咨詢18678812288
登陸網(wǎng)址:m.h6244.cn。
聯(lián)系人:王經(jīng)理。