C# LINQ干掉for循环
public void OldSum()
{
int sum0 = ;
for (int i = ; i < ; i++)
{
sum0 += i;
}
Assert.AreEqual(, sum0);
} public void NewSum()
{
int sum1 = Enumerable.Range(, ).Sum();
int sum2 = Enumerable.Range(, ).Aggregate((x, y) => x + y);
int sum3 = Enumerable.Range(, ).Aggregate(, (x, y) => x + y); Assert.AreEqual(, sum1);
Assert.AreEqual(, sum2);
Assert.AreEqual(, sum3);
}
注:无论是对一串数字求和还是求积,归根到底,都是把一串东西变成一个东西,此时就用Aggregate!
public void OldFilter()
{
int[] arr = new[] { , , , , , , , , , };
List<int> odd_list = new List<int>();
for (int i = ; i < arr.Length; i++)
{
if (arr[i] % == )
{
odd_list.Add(arr[i]);
}
}
int[] odd_arr = odd_list.ToArray();
Assert.That(odd_arr, Is.EquivalentTo(new int[] { , , , , }));
} public void NewFilter()
{
int[] arr = new[] { , , , , , , , , , };
int[] odd_arr = arr.Where(x => x % == ).ToArray();
Assert.That(odd_arr, Is.EquivalentTo(new int[] { , , , , }));
}
注:无论是取奇数还是偶数,归根到底,都是取一串东西中的某些东西,此时就用Where!
public void OldMap()
{
int[] arr = new[] { , , , , , , , , , };
List<int> new_list = new List<int>();
for (int i = ; i < arr.Length; i++)
{
new_list.Add(arr[i] * );
}
int[] new_arr = new_list.ToArray();
Assert.That(new_arr, Is.EquivalentTo(new int[] { , , , , , , , , , }));
} public void NewMap()
{
int[] arr = new[] { , , , , , , , , , };
int[] new_arr = arr.Select(x => x * ).ToArray();
Assert.That(new_arr, Is.EquivalentTo(new int[] { , , , , , , , , , }));
}
注:无论是x10还是+99,归根到底,都是把一串东西变成一串新东西,此时就用Select!
public void PrintMultiplicationFact()
{ Console.Write(
" 1 x 1= 1 \n"
+ " 1 x 2= 2 2 x 2= 4 \n"
+ " 1 x 3= 3 2 x 3= 6 3 x 3= 9 \n"
+ " 1 x 4= 4 2 x 4= 8 3 x 4=12 4 x 4=16 \n"
+ " 1 x 5= 5 2 x 5=10 3 x 5=15 4 x 5=20 5 x 5=25 \n"
+ " 1 x 6= 6 2 x 6=12 3 x 6=18 4 x 6=24 5 x 6=30 6 x 6=36 \n"
+ " 1 x 7= 7 2 x 7=14 3 x 7=21 4 x 7=28 5 x 7=35 6 x 7=42 7 x 7=49 \n"
+ " 1 x 8= 8 2 x 8=16 3 x 8=24 4 x 8=32 5 x 8=40 6 x 8=48 7 x 8=56 8 x 8=64 \n"
+ " 1 x 9= 9 2 x 9=18 3 x 9=27 4 x 9=36 5 x 9=45 6 x 9=54 7 x 9=63 8 x 9=72 9 x 9=81 \n"
); /*********************方法一: 嵌套循环*************************/
for (int j = ; j < ; j++)
{
for (int i = ; i < ; i++)
{
if (i <= j)
{
Console.Write("{0, 2} x{1, 2}={2, 2}\t", i, j, i * j);
}
}
Console.Write("\n");
} /*********************方法二: 扩展方法*************************/
Enumerable.Range(, )
.SelectMany(j => Enumerable.Range(, ), (j, i) => new
{
i, j
})
.Where(x => x.i <= x.j)
.GroupBy(x => x.j)
.Select(g => g.Aggregate("", (a, x) => a + string.Format("{0, 2} x{1, 2}={2, 2}\t", x.i, x.j, x.i * x.j)))
.ToList().ForEach(x => Console.WriteLine(x)); /*********************方法三: Linq表达式************************/
(
from j in Enumerable.Range(, )
from i in Enumerable.Range(, )
where i <= j
group new
{
i, j
}
by j into g
select new
{
LineNo = g.Key,
Line = g.Aggregate("", (a, x) => a + string.Format("{0, 2} x{1, 2}={2, 2}\t", x.i, x.j, x.i * x.j))
} ).ToList().ForEach(g => Console.WriteLine(g.Line));
}
注:对于嵌套的for循环,就用SelectMany!
C# LINQ干掉for循环的更多相关文章
- (第一篇) 一步一步带你了解linq to Object
要想学好linq to object 我们必须要先学习lambda 表达式,学习lambda 表达式呢我们必须了解匿名函数和匿名类及扩展方法,学习匿名函数,我们必须学会委托,这是本文的宗旨.下面开始第 ...
- while 循环,格式化输出和运算编码
今日内容 1.while循环 while Ture: content = input ("请输入你要喷的内容",输入Q退出) if ...
- while循环、运算符和格式化输出以及编码
一.while循环 1.while就是当的意思,while指当其后面的条件成立,就执行while下面的代码 写一段代码让程序从0打印到100的程序,每次循环+1. count = 0 while co ...
- 在net中json序列化与反序列化 面向对象六大原则 (第一篇) 一步一步带你了解linq to Object 10分钟浅谈泛型协变与逆变
在net中json序列化与反序列化 准备好饮料,我们一起来玩玩JSON,什么是Json:一种数据表示形式,JSON:JavaScript Object Notation对象表示法 Json语法规则 ...
- while循环 运算符和编码
昨日回顾 1. 初识python python是一门弱类型的解释型高级编程语言 解释器: CPython 官方提供的默认解释器. c语言实现的 PyPy 把python程序一次性进行编译. IPyth ...
- 04 Python之while循环/格式化输出/运算符/编码
1. while循环 while 条件: 循环体(break,continue) else: 循环体(break,continue) break:彻底干掉一个循环,直接跳出. continue:停止当 ...
- 控制流程之if判断与while、for循环
一.if判断 1.什么是if判断? 接收用户输入的名字 接受用户输入的密码 如果用户输入的名字=正确的名字 并且 用户输入的密码=正确的密码 告诉用户登录成功 否则, 告诉用户登录失败 2.为何要有i ...
- 整数分解、for循环阶乘
整数分解 整数分解是什么呢??我们可以这样理解 我们写一个 3位数求出它的个位十位和百位 . 那么我们来写一个小的测试来看一下! public static void main(String[] ar ...
- 高性能日志类KLog(已开源代码)
项目开源地址:https://github.com/ihambert/KLog 上回介绍了超简易日志类,但他有诸多的局限性,注定了不能作为一个网站的日志类. 那什么样的日志类才能用于网站呢.首先来假 ...
随机推荐
- GitHub的SSH key配置以及常用的git命令介绍
一. GitHub的SSH key配置 (以windows为例,Mac iOS系统类似) SSH Key 是一种方法来确定受信任的计算机,从而实现免密码登录.Git是分布式的代码管理工具,远程的代码管 ...
- 201871010131张兴盼《面向程序设计(java)》第四周学习总结
项目 内容 这个作业属于哪个课程 https://www.cnblogs.com/nwnu-daizh/ 这个作业的要求在哪里 https://www.cnblogs.com/nwnu-daizh/p ...
- Python基础之函数定义及文件修改
函数 函数的定义 #登录函数和注册函数 def register(): """注册函数""" username = input('请输入你的 ...
- node开发遇到类似:Error: ENOENT: no such file or directory, scandir 'D:\work\taro-components- ....... _node-sass@4.12.0@node-sass\vendor
唯一的有参考价值的文章,https://www.cnblogs.com/milo-wjh/p/9175138.html 我可以负责任的说,以下的方法, npm rebuild node-sass 80 ...
- ESA2GJK1DH1K升级篇: 远程升级准备工作: 使用TCP客户端连接Web服务器实现http下载数据
一,根目录建一个文件 二,使用浏览器访问 http://47.92.31.46:80/1.txt 或者 http://47.92.31.46/1.txt 三,使用TCP客户端访问文件内容 3 ...
- 【LG5444】[APIO2019]奇怪装置
[LG5444][APIO2019]奇怪装置 题面 洛谷 题目大意: 给定\(A,B\),对于\(\forall t\in \mathbb N\),有二元组\((x,y)=((t+\lfloor\fr ...
- python基础_格式化输出(%用法和format用法)(转载)
python基础_格式化输出(%用法和format用法) 目录 %用法 format用法 %用法 1.整数的输出 %o -- oct 八进制%d -- dec 十进制%x -- hex 十六进制 &g ...
- 第02组Beta冲刺(3/4)
队名:十一个憨批 组长博客 作业博客 组长黄智 过去两天完成的任务:了解整个游戏的流程 GitHub签入记录 接下来的计划:继续完成游戏 还剩下哪些任务:完成游戏 燃尽图 遇到的困难:没有美术比较好的 ...
- 安卓设备连接Mac的简单方法
mac设备是苹果出品的桌面系统,以高冷而闻名,不同于我们平常使用的windows系统,mac系统对软件硬件的兼容性很差,将iOS 设备(iPhone.iPad和iPod)连接至Mac是一件很简单的事, ...
- linux脚本中有source相关命令时的注意事项
写这个问题起因是因为一个同学去的java一键脚本环境变量设置问题, [root@localhost u01]# more 1.sh #!/bin/bash grep -q "export J ...