C#--LINQ--2--LINQ高级查询
以下是学习笔记:
1,聚合类查询
【1】
【2】
2,排序类
3,分区类
nums.Skip(1).Take(3):跳过1个取3个,所以结果就是2,3,4
4,集合类查询
感受一下:去重功能太强大了,一行代码搞定
5,生成类查询
6,总结:
以下转载
https://www.cnblogs.com/fangjb/p/15843213.html
c#中如何使用到模糊查询,先举个最简单实用的例子,可在vs控制台应用程序中输出:
定义实体类:
public class Student
{
public int ID { get; set; }
{
public int ID { get; set; }
public string Name { get; set; }
public int? Age { get; set; }
public string Sex { get; set; }
}
}
为实体类赋值存放到集合list中:
List<Student> lsStudent = new List<Student>(){
new Student { ID = 1,Name = "1", Age = 128, Sex = null },
new Student { ID = 2, Name = "2", Age = 18, Sex = "女" },
new Student { ID = 3, Name = "31", Age = 13, Sex = "男" },
new Student { ID = 4, Name = "4", Age = 23, Sex = "男" },
new Student { ID = 5, Name = "51", Age = null, Sex = "女" },
new Student { ID = 6, Name = "6", Age = null, Sex = "女" },
new Student { ID = 7, Name = "7", Age = null, Sex = "女" }
new Student { ID = 1,Name = "1", Age = 128, Sex = null },
new Student { ID = 2, Name = "2", Age = 18, Sex = "女" },
new Student { ID = 3, Name = "31", Age = 13, Sex = "男" },
new Student { ID = 4, Name = "4", Age = 23, Sex = "男" },
new Student { ID = 5, Name = "51", Age = null, Sex = "女" },
new Student { ID = 6, Name = "6", Age = null, Sex = "女" },
new Student { ID = 7, Name = "7", Age = null, Sex = "女" }
};
模糊查询条件:根据Name=1或者Sex=女模糊查询
string key = "1";
string sex = "女";
string sex = "女";
方法一:linq
List<Student> resultList = (from c in lsStudent where c.Name.Contains(key) ||c.Sex.Contains(sex) select c).ToList();
知识讲解:
1,Contains("key"),---意义等同于ql server中的like '%key%',从两端模糊匹配
2,StartsWith("key"),---意义等同于sql server中的like 'key%',从开头模糊匹配
3,EndsWith("key"),---意义等同于sql server中的like '%key',从结尾模糊匹配
要实现字符串的精确匹配,可以用 ==
from c in lsStudent where c.Name == "key"
方法二:
List<Student> resultList = lsStudent.Where(str => str.Name.Contains(key) || str.Sex.Contains(key)).ToList();
List<Student> resultList = lsStudent.Where(str => str.Name.Contains(key) || str.Sex.Contains(key)).ToList();
方法三:
List<Student> resultList = (from c in lsStudent where c.Name.IndexOf(key)>=0 ||c.Sex.IndexOf(sex)>=0 select c).ToList();
List<Student> resultList = (from c in lsStudent where c.Name.IndexOf(key)>=0 ||c.Sex.IndexOf(sex)>=0 select c).ToList();
知识讲解:
1,c.Name.IndexOf(key)>=0 ---意义等同于 like '%key%',从两端模糊匹配
2,c.Name.StartsWith(key) ---等同于like 'key%' ,从开头模糊匹配
3,c.Name.EndWith(key) ---等同于like '%key',从结尾模糊匹配
vs控制台输出打印代码:
string json = JsonConvert.SerializeObject(resultList, Newtonsoft.Json.Formatting.Indented);
Console.WriteLine(json);
Console.WriteLine(json);
https://www.cnblogs.com/alunchen/p/6829460.html
List<User> users1 = new List<User>(); users1.Add(new User() {Id = 1, Age = 1, Name = "Name01"}); users1.Add(new User() { Id = 2, Age = 2, Name = "Name02" }); users1.Add(new User() { Id = 3, Age = 3, Name = "Name03" }); users1.Add(new User() { Id = 4, Age = 4, Name = "Name04" }); List<User> users2 = new List<User>(); users2.Add(new User() { Id = 4, Age = 44, Name = "Name044" }); users2.Add(new User() { Id = 5, Age = 55, Name = "Name01" }); users2.Add(new User() { Id = 6, Age = 66, Name = "Name06" }); users2.Add(new User() { Id = 7, Age = 77, Name = "Name07" }); users2.Add(new User() { Id = 8, Age = 88, Name = "Name08" }); //把两个list合并在一起 var result1 = new List<User>(); result1.AddRange(users1); result1.AddRange(users2); //去重:在result1中去重 var result2 = result1.Distinct().ToList(); //差集:users1在users2之中的差集 var result3 = users1.Except(users2).ToList(); //并集:users1与users2 合起来不重复的元素 var result4 = users1.Union(users2).ToList(); //交集:users1与users2 相同的元素 var result5 = users1.Intersect(users2).ToList();