這一節大家共同學習下LINQ的基本用法,主要包括LINQ的分組,排序,和內外連接。
1.分組
基本語法: group element by key
element 表示查詢結果返回的元素,key表示分組條件。group子句返回的類型為IGrouping<TKey,TElement>的查詢結果。
IGrouping<TKey,TElement>可以看成一個hashtable內部嵌套一個list列表的數據結果. 其中TElement就是一個list
我們看下面的一個事例,下面這個實例演示根據性別對用戶進行分組
代碼
[csharp]
public class User
{
public User(string userName,string sex,int age)
{
UserName = userName;
Sex = sex;
Age = age;
}
public string UserName
{
get;
set;
}
public string Sex
{
get;
set;
}
public int Age
{
get;
set;
}
}
public class User
{
public User(string userName,string sex,int age)
{
UserName = userName;
Sex = sex;
Age = age;
}
public string UserName
{
get;
set;
}
public string Sex
{
get;
set;
}
public int Age
{
get;
set;
}
}
[csharp]
private void button1_Click(object sender, EventArgs e)
{
User[] users = {
new User("劉德華","男",40),
new User("張學友","男",30),
new User("張曼玉","女",35),
new User("楊冪","女",53)
};
var query1 =
from var1 in users
group var1 by var1.Sex;
string str = "";
foreach (var item in query1)
{
str += item.Key + "\r\n";
foreach (var user in item)
{
str += user.UserName + "--" + user.Sex + "--" + user.Age.ToString() + "\r\n";
}
}
textBox1.Text = str;
}
private void button1_Click(object sender, EventArgs e)
{
User[] users = {
new User("劉德華","男",40),
new User("張學友","男",30),
new User("張曼玉","女",35),
new User("楊冪","女",53)
};
var query1 =
from var1 in users
group var1 by var1.Sex;
string str = "";
foreach (var item in query1)
{
str += item.Key + "\r\n";
foreach (var user in item)
{
str += user.UserName + "--" + user.Sex + "--" + user.Age.ToString() + "\r\n";
}
}
textBox1.Text = str;
}
效果圖
以上是簡單的分組,如果需要對分組結果進行排序,需要把分組結果保存到一個臨時變量中
語法: group element by key into temp
以下是按年齡從小到大的排序:
代碼
[csharp]
User[] users = {
new User("劉德華","男",40),
new User("張學友","男",30),
new User("張曼玉","女",35),
new User("楊冪","女",53)
};
var query1 =
from var1 in users
group var1 by var1.Age into temp
orderby temp.Key descending
select temp;
string str = "";
foreach (var item in query1)
{
str += item.Key + "\r\n";
foreach (var user in item)
{
str += user.UserName + "--" + user.Sex + "--" + user.Age.ToString() + "\r\n";
}
}
textBox1.Text = str;
User[] users = {
new User("劉德華","男",40),
new User("張學友","男",30),
new User("張曼玉","女",35),
new User("楊冪","女",53)
};
var query1 =
from var1 in users
group var1 by var1.Age into temp
orderby temp.Key descending
select temp;
string str = "";
foreach (var item in query1)
{
str += item.Key + "\r\n";
foreach (var user in item)
{
str += user.UserName + "--" + user.Sex + "--" + user.Age.ToString() + "\r\n";
}
}
textBox1.Text = str;
效果圖
2.排序
在linq中排序使用orderby
語法 orderby elment descending||ascending
ascending表示升序,descending表示降序
下面這個DEMO演示按年齡從大到小排序
代碼:
[csharp]
User[] users = {
new User("劉德華","男",40),
new User("張學友","男",30),
new User("張曼玉","女",35),
new User("楊冪","女",53)
};
var query1 =
from var1 in users
orderby var1.Age descending
select var1;
string str = "";
foreach (var item in query1)
{
str += item.UserName + "--" + item.Age + "\r\n";
}
textBox1.Text = str;
User[] users = {
new User("劉德華","男",40),
new User("張學友","男",30),
new User("張曼玉","女",35),
new User("楊冪","女",53)
};
var query1 =
from var1 in users
orderby var1.Age descending
select var1;
string str = "";
foreach (var item in query1)
{
str += item.UserName + "--" + item.Age + "\r\n";
}
textBox1.Text = str;
效果圖
3.內連接
內連接查詢就是根據一定的條件查詢兩個數據源中都存在的元素
語法: join elment in datasource on re1 equals re2
以下代碼查找兩個數組中都存在的元素。
代碼:
[csharp]
private void button3_Click(object sender, EventArgs e)
{
//查詢兩個數組中都存在的元素
int[] arrOne = { 10,20,30,6,8};
int[] arrTwo = { 10,4,6,8,90,3};
var query = from re1 in arrOne
join re2 in arrTwo on re1 equals re2
select new { re1 = re1, re2 = re2 };
foreach (var item in query)
{
Console.WriteLine(item);
}
}
private void button3_Click(object sender, EventArgs e)
{
//查詢兩個數組中都存在的元素
int[] arrOne = { 10,20,30,6,8};
int[] arrTwo = { 10,4,6,8,90,3};
var query = from re1 in arrOne
join re2 in arrTwo on re1 equals re2
select new { re1 = re1, re2 = re2 };
foreach (var item in query)
{
Console.WriteLine(item);
}
}
查詢結果:
[csharp]
{ re1 = 10, re2 = 10 }
{ re1 = 6, re2 = 6 }
{ re1 = 8, re2 = 8 }
{ re1 = 10, re2 = 10 }
{ re1 = 6, re2 = 6 }
{ re1 = 8, re2 = 8 }
4.左連接
左連接和SQL中的左連接類似,返回第一個結果集中的所有元素。
下面的代碼演示使用左連接,左連接要用到defaultempty()方法,此方法從列表中獲取指定元素,如果列表為空,返回默認值
代碼
[csharp]
private void button4_Click(object sender, EventArgs e)
{
//左連接
int[] arrOne = { 10, 20, 30, 6, 8 };
int[] arrTwo = { 10, 4, 6, 8, 90, 3 };
var query = from re1 in arrOne
join re2 in arrTwo on re1 equals re2 into temgrp
from grp in temgrp.DefaultIfEmpty()
select new { re1 = re1, re2 = grp };
foreach (var item in query)
{
Console.WriteLine(item);
}
}
private void button4_Click(object sender, EventArgs e)
{
//左連接
int[] arrOne = { 10, 20, 30, 6, 8 };
int[] arrTwo = { 10, 4, 6, 8, 90, 3 };
var query = from re1 in arrOne
join re2 in arrTwo on re1 equals re2 into temgrp
from grp in temgrp.DefaultIfEmpty()
select new { re1 = re1, re2 = grp };
foreach (var item in query)
{
Console.WriteLine(item);
}
}
查詢結果
[csharp] view plaincopyprint?{ re1 = 10, re2 = 10 }
{ re1 = 20, re2 = 0 }
{ re1 = 30, re2 = 0 }
{ re1 = 6, re2 = 6 }
{ re1 = 8, re2 = 8 }
{ re1 = 10, re2 = 10 }
{ re1 = 20, re2 = 0 }
{ re1 = 30, re2 = 0 }
{ re1 = 6, re2 = 6 }
{ re1 = 8, re2 = 8 }
作者:zx13525079024