Lambda表达式练习
2007-04-21 12:39 Clingingboy 阅读(892) 评论(1) 编辑 收藏 举报
根据微软的示例代码,体验一下
1.准备测试数据
static int[] numbers = new int[] { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 };
static string[] strings = new string[] { "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"};
class Person {
public string Name;
public int Level;
}
static Person[] persons = new Person[] {
new Person {Name="Matt", Level=3},
new Person {Name="Luca", Level=3},
new Person {Name="Jomo", Level=5},
new Person {Name="Dinesh", Level=3},
new Person {Name="Charlie", Level=3},
new Person {Name="Mads", Level=3},
new Person {Name="Anders", Level=9}
};
2.过滤数据
public static void Sample1() {
// use Where() to filter out elements matching a particular condition
IEnumerable<int> fnums = numbers.Where(n => n < 5);
Console.WriteLine("Numbers < 5");
foreach(int x in fnums) {
Console.WriteLine(x);
}
}
3.匹配首个字母
public static void Sample2() {
// use First() to find the one element matching a particular condition
string v = strings.First(s => s[0] == 'o');
Console.WriteLine("string starting with 'o': {0}", v);
}
4.根据numbers排序
public static void Sample3() {
// use Select() to convert each element into a new value
IEnumerable<string> snums = numbers.Select(n => strings[n]);
Console.WriteLine("Numbers");
foreach(string s in snums) {
Console.WriteLine(s);
}
}
5.匿名类型,注意var关键字
public static void Sample4()
{
// use Anonymous Type constructors to construct multi-valued results on the fly
var q = strings.Select(s => new {Head = s.Substring(0,1), Tail = s.Substring(1)});
foreach(var p in q) {
Console.WriteLine("Head = {0}, Tail = {1}", p.Head, p.Tail);
}
}
6.联合查询(即使用两个以上的查询条件)
public static void Sample5() {
// Combine Select() and Where() to make a complete query
var q = numbers.Where(n => n < 5).Select(n => strings[n]);
Console.WriteLine("Numbers < 5");
foreach(var x in q) {
Console.WriteLine(x);
}
}
7.使用ToList方法缓存数据,不知道这样说对不对
public static void Sample6() {
// Sequence operators form first-class queries are not executed until you enumerate them.
int i = 0;
var q = numbers.Select(n => ++i);
// Note, the local variable 'i' is not incremented until each element is evaluated (as a side-effect).
foreach(var v in q) {
Console.WriteLine("v = {0}, i = {1}", v, i);
}
Console.WriteLine();
// Methods like ToList() cause the query to be executed immediately, caching the results
int i2 = 0;
var q2 = numbers.Select(n => ++i2).ToList();
// The local variable i2 has already been fully incremented before we iterate the results
foreach(var v in q2) {
Console.WriteLine("v = {0}, i2 = {1}", v, i2);
}
}
8.分组查询
public static void Sample7() {
// use GroupBy() to construct group partitions out of similar elements
var q = strings.GroupBy(s => s[0]); // <- group by first character of each string
foreach(var g in q) {
Console.WriteLine("Group: {0}", g.Key);
foreach(string v in g) {
Console.WriteLine("\tValue: {0}", v);
}
}
}
9.统计聚合
public static void Sample8() {
// use GroupBy() and aggregates such as Count(), Min(), Max(), Sum(), Average() to compute values over a partition
var q = strings.GroupBy(s => s[0]).Select(g => new {FirstChar = g.Key, Count = g.Count()});
foreach(var v in q) {
Console.WriteLine("There are {0} string(s) starting with the letter {1}", v.Count, v.FirstChar);
}
}
10.排序
// use OrderBy()/OrderByDescending() to give order to your resulting sequence
var q = strings.OrderBy(s => s); // order the strings by their name
foreach(string s in q) {
Console.WriteLine(s);
}
}
11.二次排序
public static void Sample9a() {
// use ThenBy()/ThenByDescending() to provide additional ordering detail
var q = persons.OrderBy(p => p.Level).ThenBy(p => p.Name);
foreach(var p in q) {
Console.WriteLine("{0} {1}", p.Level, p.Name);
}
}
1.准备测试数据

















2.过滤数据









3.匹配首个字母






4.根据numbers排序










5.匿名类型,注意var关键字








6.联合查询(即使用两个以上的查询条件)









7.使用ToList方法缓存数据,不知道这样说对不对


















8.分组查询











9.统计聚合








10.排序







11.二次排序








【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 25岁的心里话
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现
2006-04-21 初步学习petshop
2006-04-21 官方msdn2005中文版文档出了
2006-04-21 开始准备学习petshop3.0