特性:
语法介绍:
- from子句:指定查询操作的数据源和范围变量
- where子句:筛选元素的逻辑条件,返回值是一个bool类型
- select子句:指定查询结果的类型和表现形式
- orderby子句:对查询结果进行排序
- group子句:对查询结果进行分组
- into子句:提供一个临时标识符,该表示可充当对join/group/select子句结果的引用
- join子句:连接多个查询操作的数据源
- let子句:引入用户存储查询表达式中的子表达式结果的范围变量
示例:
int[] nums = { 1, 7, 2, 6, 9, 5, 4, 6, 45 };
List<string> list = new List<string>() { "1", "22", "333", "4444" };
User[] users = { new User { Name = "张三", Age = 23 }, new User { Name = "李四", Age = 24 }, new User { Name = "王五", Age = 25 } };
// 查询子句:linq查询表达式必须以from子句开头,并且必须以select或group子句结束,中间可以添加多个子句
var lit8 = from num in nums
where num % 2 != 0
orderby num descending
select num;
// Select
var list3 = nums.Select(item => item * item);
var intlists = list.Select(it => int.Parse(it)).ToList();//转为int类型list
var intlists2 = list.Select(int.Parse).ToList();//简化lambda
var names = users.Select(item => item.Name);// 获取name数组
// Where
var list1 = from m in list where m.Length == 3 select m;// 查询语句
var list2 = list.Where(m => m.Length == 2);// 使用lambda(查询方法)
// OrderBy()
var list4 = from m in nums orderby m ascending select m;
var list5 = nums.OrderByDescending(m => m).ToList();
var list6 = list.OrderByDescending(item => item.Substring(0, 1));
// GroupBy() 分组
var list7 = list.GroupBy(item => item.Substring(0, 1));
foreach (var groupItem in list7)
{
Console.WriteLine("分组字段:{0}", groupItem.Key);
foreach (var item in groupItem)
{
Console.WriteLine(item);
}
}
// 复合查询
var result = from u in users
from score in u.ScoreList
where score >= 90
select u;
var result = from u1 in users1
where u1.Age > 20
from u2 in users2
where u2.Age > 20
select new { u1, u2 };
// OfType 查询特定类型
var list8 = list.OfType<string>().ToList();
// Join 合并两集合通过指定键,返回指定结构类型集合
// GroupJoin 俩集合通过指定键分组
List<Stu> list3 = new List<Stu>() { new Stu(1, "Tom"), new Stu(2, "Lisa"), new Stu(3, "Jake"), new Stu(4, "auston") };
List<string> list4 = new List<string>() { "Semi", "Lisa", "auston" };
var list5 = list3.Join(list4, p => p.Name, s => s, (p, s) => new { NewID = p.Id, NewName = s }).ToList();
foreach (var item in list5) { Console.WriteLine(item.NewName); }
var list6 = list3.GroupJoin(list4, p => p.Name, s => s, (p, result) => new { plist = p.Name, count = result.Count() });
// Any / All 判断是否(任意一个/全部)满足条件
bool isOk = users.Any(p => p.Age == 23);
bool isOk2 = users.All(p => p.Age > 22);
// Skip 跳过指定数量元素
var list10 = list.Skip(3).ToList();
// Take 拿取指定数量元素
var list11 = nums.Skip(3).Take(2);
// TakeWhile 只要满足指定的条件,就会返回序列的元素,然后跳过剩余的元素
// SkipWhile 只要满足指定的条件,就跳过序列中的元素,然后返回剩余元素
var list12 = nums.SkipWhile(i => i % 3 != 0)
.TakeWhile(i => i % 2 != 9);
// Count 获取元素个数
var count1 = (from c in stuList
where c.StuId > 1010
select c).Count();
var count2 = stuList.Where(c => c.StuId > 1010).Count();
// Sum、Average、Max、Min获取集合总值、平均值、最大值、最小值
var maxAge = (from u in users
select u.Age).Max();
var minAge = (from u in users
select u.Age).Min();
var avgAge = (from u in users
select u.Age).Average();
var sumAge = (from u in users
select u.Age).Sum();
// ThenBy 提供复合排序条件
var users1 = from u in users
orderby u.Name, u.Age
select u;
var users2 = users
.OrderBy(u => u.Name)
.ThenBy(u => u.Age)
.Select(u => u);
// Concat连接集合
var list14 = list11.Concat(list12).Distinct();
// Distinct 去掉集合中的重复项()
var list13 = nums.Distinct();
// 类中可以使用groupby去重
var persons = new List<Person> {
new Person{ Id = 3, Name = "张三", Age = 3 },
new Person{ Id = 4, Name = "李四", Age = 4 },
new Person{ Id = 5, Name = "王五", Age = 5 }
};
var plist = persons.GroupBy(p => p.Id)
.Select(g => g.First())
.ToList();
//类中重写Equals()和GetHashCode()方法后使用Distinct去重
var plist2 = persons.Distinct().ToList();
// Range 生成一个整数序列
var nums11 = Enumerable.Range(1, 10);
// Repeat 生成一个重复项的序列
var nums12 = Enumerable.Repeat("linq", 10);
// ElementAt获取指定索引元素(与[ ]类似)
var stu = list3.ElementAt(3);
// First/Single、Last:获取集合中第一个、最后一个元素(如果集合中包含多个元素,使用Single会报错)
// ToDictionary:将集合转换为字典
// ToList: 将集合转换为List
// SequenceEqual:判断两个集合是否相等