目录
3、方式三 -- 扩展方法 + Lambda方法 缩减版 -- 推荐
涉及知识点: 扩展方法、匿名类、Where、Select、Join ……
一、查询集合方式
题目:查询Id>=2的数据
1、方式一 -- 普通查询
代码编写:
internal class Program
{
static void Main(string[] args)
{
List<Student> students = new List<Student>()
{
new Student(1,"小王"),
new Student(2,"小白"),
new Student(3,"小红"),
new Student(4,"小粉")
};
//↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓
foreach (Student student in students)
{
if (student.Id >= 2)
{
Console.WriteLine($"{student.Id} {student.Name}");
}
}
//↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑
Console.WriteLine("Hello World!");
}
}
class Student
{
public int Id { set; get; }
public string Name { set; get; }
public Student(int id, string name)
{
this.Id = id;
this.Name = name;
}
}
结果显示:
2、方式二 -- 扩展方法 + 委托 + 普通方法 完整版
代码编写:
internal class Program
{
static void Main(string[] args)
{
List<Student> students = new List<Student>()
{
new Student(1,"小王"),
new Student(2,"小白"),
new Student(3,"小红"),
new Student(4,"小粉")
};
//↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓
Func<Student, bool> func = new Func<Student, bool>(GetAge);
List<Student> list = students.Where(func).ToList();
foreach (Student student in list)
{
Console.WriteLine($"{student.Id} {student.Name}");
}
//↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑
Console.WriteLine("Hello World!");
}
//↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓
static bool GetAge(Student student)
{
if (student.Id >= 2)
{
return true;
}
return false;
}
//↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑
}
class Student
{
public int Id { set; get; }
public string Name { set; get; }
public Student(int id, string name)
{
this.Id = id;
this.Name = name;
}
}
结果显示:
3、方式三 -- 扩展方法 + Lambda方法 缩减版 -- 推荐
代码编写:
internal class Program
{
static void Main(string[] args)
{
List<Student> students = new List<Student>()
{
new Student(1,"小王"),
new Student(2,"小白"),
new Student(3,"小红"),
new Student(4,"小粉")
};
//↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓
List<Student> list = students.Where(x => x.Id >= 2).ToList();
foreach (Student student in list)
{
Console.WriteLine($"{student.Id} {student.Name}");
}
//↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑
Console.WriteLine("Hello World!");
}
}
class Student
{
public int Id { set; get; }
public string Name { set; get; }
public Student(int id, string name)
{
this.Id = id;
this.Name = name;
}
}
结果显示:
4、方式四 -- SQL表达式查询
代码编写:
internal class Program
{
static void Main(string[] args)
{
List<Student> students = new List<Student>()
{
new Student(1,"小王"),
new Student(2,"小白"),
new Student(3,"小红"),
new Student(4,"小粉")
};
//↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓
List<Student> list = (from s in students
where s.Id >= 2
select s).ToList();
foreach (Student student in list)
{
Console.WriteLine($"{student.Id} {student.Name}");
}
//↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑
Console.WriteLine("Hello World!");
}
}
class Student
{
public int Id { set; get; }
public string Name { set; get; }
public Student(int id, string name)
{
this.Id = id;
this.Name = name;
}
}
结果显示:
二、MyWhere扩展方法编写
代码编写:
internal class Program
{
static void Main(string[] args)
{
List<Student> students = new List<Student>()
{
new Student(1,"小王"),
new Student(2,"小白"),
new Student(3,"小红"),
new Student(4,"小粉")
};
List<Student> list = students.MyWhere(x => x.Id >= 2);
foreach (Student student in list)
{
Console.WriteLine($"{student.Id} {student.Name}");
}
Console.WriteLine("Hello World!");
}
}
class Student
{
public int Id { set; get; }
public string Name { set; get; }
public Student(int id, string name)
{
this.Id = id;
this.Name = name;
}
}
static class StudentExtend
{
public static List<Student> MyWhere(this List<Student> sources,
Func<Student, bool> predicate)
{
List<Student> list = new List<Student>();
foreach (Student source in sources)
{
if (predicate(source)) // 等同于 if(predicate.Invoke(source))
{
list.Add(source);
}
}
return list;
}
}
结果显示:
三、Select投影应用 -- 扩展方法
代码编写:
internal class Program
{
static void Main(string[] args)
{
List<Student> students = new List<Student>()
{
new Student(1,"小王"),
new Student(2,"小白"),
new Student(3,"小红"),
new Student(4,"小粉")
};
List<Student> students_1 = new List<Student>()
{
new Student(1,"老板"),
new Student(2,"员工")
};
//↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓
//查询全部
List<Student> list = students.Select(x => x).ToList();
//查询id
List<int> list_1 = students.Select(x => x.Id).ToList();
//查询id,name
List<(int Id, string Name)> list_2 = students
.Select(x => (x.Id, x.Name))
.ToList();
//匿名类查询
var list_3 = students.Select(x => new { x.Id }).ToList();
foreach (Student student in list)
{
Console.WriteLine($"{student.Id} {student.Name}");
}
//↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑
Console.WriteLine("Hello World!");
}
}
class Student
{
public int Id { set; get; }
public string Name { set; get; }
public Student(int id, string name)
{
this.Id = id;
this.Name = name;
}
}
结果显示:
四、Join多表查询
两张表以上建议用join语句
1、创建新类存放 -- 完整版
代码编写:
internal class Program
{
static void Main(string[] args)
{
List<Student> students = new List<Student>()
{
new Student(1,"小王"),
new Student(2,"小白"),
new Student(3,"小红"),
new Student(4,"小粉")
};
List<School> school = new List<School>()
{
new School(1,"老板"),
new School(2,"员工")
};
//↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓
List<Result> list = students.Join(school, //副表
x => x.Id, //主表连接条件
s => s.Id, //副表连接条件
(s, x) => //新表赋值
new Result()
{
Id = s.Id,
StudentName = s.Name,
SchoolName = x.Name,
}).ToList();
foreach (Result student in list)
{
Console.WriteLine($"{student.Id} " +
$"{student.StudentName} " +
$"{student.SchoolName}");
}
//↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑
Console.WriteLine("Hello World!");
}
}
class Student
{
public int Id { set; get; }
public string Name { set; get; }
public Student(int id, string name)
{
this.Id = id;
this.Name = name;
}
}
class School
{
public int Id { set; get; }
public string Name { set; get; }
public School(int id, string name)
{
this.Id = id;
this.Name = name;
}
}
//↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓
class Result
{
public int Id { set; get; }
public string StudentName { set; get; }
public string SchoolName { set; get; }
}
//↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑
#endregion
结果显示:
2、使用匿名类,缩减一个类 -- 推荐
代码编写:
internal class Program
{
static void Main(string[] args)
{
List<Student> students = new List<Student>()
{
new Student(1,"小王"),
new Student(2,"小白"),
new Student(3,"小红"),
new Student(4,"小粉")
};
List<School> school = new List<School>()
{
new School(1,"老板"),
new School(2,"员工")
};
//↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓
var list = students.Join(school, //副表
x => x.Id, //主表连接条件
s => s.Id, //副表连接条件
(s, x) => //新表赋值
new
{
Id = s.Id,
StudentName = s.Name,
SchoolName = x.Name,
}).ToList();
foreach (var student in list)
{
Console.WriteLine($"{student.Id} " +
$"{student.StudentName} " +
$"{student.SchoolName}");
}
//↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑
Console.WriteLine("Hello World!");
}
}
class Student
{
public int Id { set; get; }
public string Name { set; get; }
public Student(int id, string name)
{
this.Id = id;
this.Name = name;
}
}
class School
{
public int Id { set; get; }
public string Name { set; get; }
public School(int id, string name)
{
this.Id = id;
this.Name = name;
}
}
结果显示:
3、SQL + 匿名类
SQL中 相等必须用equals
代码编写:
internal class Program
{
static void Main(string[] args)
{
List<Student> students = new List<Student>()
{
new Student(1,"小王"),
new Student(2,"小白"),
new Student(3,"小红"),
new Student(4,"小粉")
};
List<School> school = new List<School>()
{
new School(1,"老板"),
new School(2,"员工")
};
//↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓
var list =(from s in students //主表
join x in school //副表
on s.Id equals x.Id //连接条件
select new //返回列表
{
Id = s.Id,
StudentName = s.Name,
SchoolName = x.Name
}).ToList();
foreach (var student in list)
{
Console.WriteLine($"{student.Id} " +
$"{student.StudentName} " +
$"{student.SchoolName}");
}
//↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑
Console.WriteLine("Hello World!");
}
}
class Student
{
public int Id { set; get; }
public string Name { set; get; }
public Student(int id, string name)
{
this.Id = id;
this.Name = name;
}
}
class School
{
public int Id { set; get; }
public string Name { set; get; }
public School(int id, string name)
{
this.Id = id;
this.Name = name;
}
}
结果显示:
五、Linq其他方法
Take(2) -- 查询前2条信息
Skip(2) -- 跳过2条信息
OrderBy(x=>x.Id) -- 根据Id顺序排序
OrderByDescending(x=>x.Id) -- 根据Id逆序排序
Contains("小王") -- 包含"小王"的数据
FirstOrDefault() -- 返回第一条数据,若无数据,则返回默认值
代码编写:
internal class Program
{
static void Main(string[] args)
{
List<Student> students = new List<Student>()
{
new Student(1,"小王"),
new Student(2,"小白"),
new Student(3,"小红"),
new Student(4,"小粉")
};
//↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓
//查询前2条信息
List<Student> list = students.Take(2).ToList();
//跳过一条 查后一条信息
List<Student> list_1 = students.Skip(1).Take(1).ToList();
//根据Id顺序排序
List<Student> list_2 = students.OrderBy(x => x.Id).ToList();
//根据Id逆序排序
List<Student> list_3 = students.OrderByDescending(x => x.Id).ToList();
//查询包含"小王"的数据
List<Student> list_4 = students.Where(x => x.Name.Contains("小王")).ToList();
//返回第一条信息
Student list_5 = students.FirstOrDefault();
foreach (Student student in list)
{
Console.WriteLine($"{student.Id} {student.Name}");
}
//↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑
Console.WriteLine("Hello World!");
}
}
class Student
{
public int Id { set; get; }
public string Name { set; get; }
public Student(int id, string name)
{
this.Id = id;
this.Name = name;
}
}
结果显示:
如有错误,烦请批评指正