文章目录
1. linq简介
LINQ定义了大约40个查询操作符,如select、from、in、where以及order by(C#中)。使用这些操作符可以编写查询语句。不过,这些查询还可以基于很多类型的数据,每个数据类型都需要一个单独的LINQ类型。
注:从对象获取数据的方法
2. 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 > 3 select n;
foreach (int x in lowNums) { Console.WriteLine($"{x},"); }
}
示例中的linq代码数据源是数组,但Linq还支持多种的数据源格式
3. 匿名类型(LinQ多用于匿名类型)
static void Main(string[] args){
var student = new { name = "jamin" ,age=20,major= "ComputerScience" };
Console.WriteLine($"name: {student.name},age: {student.age},major: {student.major}");
}
注:
匿名类只能在局部变量中使用,不可以在类成员中使用。
必须使用var作为变量类型。
不能设置匿名类型对象的属性。
2、匿名类型的其他初始化方式
internal class Program
{
static void Main(string[] args)
{
string major = "ComputerScience";
//成员访问、赋值、标识符
var student = new { Other.name,age=20,major };
Console.WriteLine($"name: {student.name},age: {student.age},major: {student.major}");
}
}
class Other
{
static public string name = "Jamin";
}
4、Linq的方法语法和查询语法
注:两种方法可以同时使用
1、方法语法
static void Main(string[] args){
int[] numbers = { 1, 2, 3, 4, 5, 6, 7, 8 };
//方法语法
var numMethod = numbers.Where(n => n > 3);
foreach (int x in lowNums) { Console.WriteLine($"{x},"); }
}
2、查询语法
static void Main(string[] args){
int[] numbers = { 1, 2, 3, 4, 5, 6, 7, 8 };
//方法语法
var numQuery = from n in numbers where n > 3 select n;
foreach (int x in lowNums) { Console.WriteLine($"{x},"); }
}
3、两种结合
static void Main(string[] args){
int[] numbers = { 1, 2, 3, 4, 5, 6, 7, 8 };
//两种形式结合
int numCount = (from n in numbers where n > 3 select n).Count();
Console.WriteLine(numCount);
}
5、Linq和foreach的区别
1、foreach命令式的指定从第一个到最后一个按顺序访问集合中的项。
Linq也是每个项都要访问但是没有规定顺序2、foreach在遇到代码的时候就执行主体,而Linq创建可以执行查询的后台代码对象只有遇到访问查询变量的语句时才会执行查询。
6、JOIN子句
linq中的join接受两个集合,然后创建一个新的集合,其中每一个元素包含两个原始集合中的元素成员。
static void Main(string[] args)
{
var query = from s in students join c in courses on s.id equals c.id
where s.name == "c"
select c.courseName;
foreach (var item in query)
{
Console.WriteLine($"C的课程有{item}");
}
}
static Student[] students = new Student[]
{
new Student {id=1,name="c"},
new Student {id=2,name="z"},
new Student {id=3,name="m"}
};
static Course[] courses = new Course[] {
new Course{courseName="CS",id=1},
new Course{courseName="history",id=2},
new Course{courseName="Chinese",id=1}
};
}
class Student
{
public int id;
public string name;
}
public class Course
{
public int id;
public string courseName;
}
7、查询主体中的from…let…where片段
static void Main(string[] args)
{
var groupA = new[] { 1,2,3,4,5};
var groupB = new[] {6,7,8,9,10 };
var someInts=from a in groupA
from b in groupB
where a>3&&b<8
select new { a, b, sum =a+b};//匿名类型对象
foreach (var item in someInts)
{
Console.WriteLine(item);
}
}
2、let子句
static void Main(string[] args)
{
var groupA = new[] { 1,2,3,4,5};
var groupB = new[] {6,7,8,9,10 };
var someInts=from a in groupA
from b in groupB
let sum =a+b
where sum==12
select new { a, b, sum};//匿名类型对象
foreach (var item in someInts)
{
Console.WriteLine(item);
}
}
3、where子句
static void Main(string[] args)
{
var groupA = new[] { 1, 2, 3, 4, 5 };
var groupB = new[] { 6, 7, 8, 9, 10 };
var someInts = from a in groupA
from b in groupB
let sum = a + b
where sum == 12
where a == 4
select new { a, b, sum };
foreach (var item in someInts)
{
Console.WriteLine(item);
}
}
8、Orderby子句
ascending升序descending降序,进行显式的进行排序(默认升序)
static void Main(string[] args)
{
var query = from s in students
orderby s.id descending
select s.name;
foreach (var item in query)
{
Console.WriteLine($"{item}");
}
}
static Student[] students = new Student[]
{
new Student {id=1,name="c"},
new Student {id=2,name="z"},
new Student {id=3,name="m"}
};
class Student
{
public int id;
public string name;
}
9、group by
static void Main(string[] args)
{
var query = from s in students
group s by s.name;
foreach (var item in query)
{
Console.WriteLine($"{item.Key}");
foreach (var item2 in item)
Console.WriteLine($"{item2.id},{item2.name}");
}
}
static Student[] students = new Student[]
{
new Student {id=1,name="c"},
new Student {id=2,name="z"},
new Student {id=3,name="m"},
new Student {id=4,name="c"}
};
class Student
{
public int id;
public string name;
}
students = new Student[]
{
new Student {id=1,name="c"},
new Student {id=2,name="z"},
new Student {id=3,name="m"},
new Student {id=4,name="c"}
};
class Student
{
public int id;
public string name;
}
---------------待续