C# LINQ
Posted HelloCopyer
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了C# LINQ相关的知识,希望对你有一定的参考价值。
LINQ允许我们以使用SQL查询数据库的方式来查询数据集合。下面我们查看一段代码,代码演示了如何使用LINQ的示例。
static void Main(string[] args)
{
int[] numbers = { 1, 2, 3, 4,5,6,7,8 }; //数据源
IEnumerable<int> lowNums = //定义并存储查询
from n in numbers
where n < 4
select n;
foreach (var x in lowNums) //执行查询
Console.Write("{0}, ", x);
}
代码产生了如下输出:
1, 2, 3,
语句介绍
from
join
let
where
orderby
group
from
from子句是最基本的,基本语法如下:from 变量 in 集合
int[] arr = { 1, 2, 3 };
var query = from n in arr
where n < 2
select n;
foreach (var item in query)
Console.WriteLine(item);
输出结果
1
join
连接多个集合的数据
连接
public class A
{
public string name;
public string id;
}
public class B
{
public string sex;
public string id;
}
static A[] aS = new A[]
{
new A{ name = "小明",id = "01"},
new A{ name = "小白",id = "02"},
new A{ name = "小黑",id = "03"},
};
static B[] bS = new B[]
{
new B{ sex = "男",id = "01"},
new B{ sex = "男",id = "02"},
new B{ sex = "女",id = "03"},
};
static void Main(string[] args)
{
var quert = from a in aS
join b in bS on a.id equals b.id
where b.sex == "男"
select a.name;
foreach (var item in quert)
{
Console.WriteLine("性别为男的姓名:" + item);
}
Console.ReadKey();
}
输出结果:
性别为男的姓名:小明
性别为男的姓名:小白
let
static void Main(){
var groupA = new[] { 3, 4, 5, 6 };
var groupB = new[] { 6, 7, 8, 9 };
var someInts = from a in groupA
from b in groupB
let sum = a + b ←在新的变量中保存结果
where sum == 12
select new {a, b, sum};
foreach (var a in someInts)
Console.WriteLine(a);}
输出结果
{ a = 3, b = 9, sum = 12 }
{ a = 4, b = 8, sum = 12 }
{ a = 5, b = 7, sum = 12 }
{ a = 6, b = 6, sum = 12 }
where
static void Main(){
var groupA = new[] { 3, 4, 5, 6 };
var groupB = new[] { 6, 7, 8, 9 };
var someInts = from int a in groupA
from int b in groupB
let sum = a + b
where sum >= 11 ←条件1
where a == 4 ←条件2
select new {a, b, sum};
foreach (var a in someInts)
Console.WriteLine(a);}
输出结果
{ a = 4, b = 7, sum = 11 }
{ a = 4, b = 8, sum = 12 }
{ a = 4, b = 9, sum = 13 }
orderby
static void Main( ) {
var students = new [] //匿名类型的对象数组
{
new { LName="Jones", FName="Mary", Age=19, Major="History" },
new { LName="Smith", FName="Bob", Age=20, Major="CompSci" },
new { LName="Fleming", FName="Carol", Age=21, Major="History" }
};
var query = from student in students
orderby student.Age ←根据Age排序
select student;
foreach (var s in query) {
Console.WriteLine("{0}, {1}: {2} - {3}",
s.LName, s.FName, s.Age, s.Major);
}}
输出结果:
Jones, Mary: 19 – HistorySmith,
Bob: 20 – CompSciFleming,
Carol: 21 – History
group
static void Main( ){
var students = new[] //匿名类型的对象数组
{
new { LName="Jones", FName="Mary", Age=19, Major="History" },
new { LName="Smith", FName="Bob", Age=20, Major="CompSci" },
new { LName="Fleming", FName="Carol", Age=21, Major="History" }
};
var query = from student in students
group student by student.Major;
foreach (var s in query) //枚举分组
{
Console.WriteLine("{0}", s.Key);
--↑--
分组键
foreach (var t in s) //枚举分组中的项
Console.WriteLine(" {0}, {1}", t.LName, t.FName);
}}
输出结果:
History
Jones, Mary
Fleming,
CarolCompSci
Smith, Bob
以上是关于C# LINQ的主要内容,如果未能解决你的问题,请参考以下文章