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 上回介绍了超简易日志类,但他有诸多的局限性,注定了不能作为一个网站的日志类. 那什么样的日志类才能用于网站呢.首先来假 ...
随机推荐
- 视频合并时使用python批量修改文件名
不知道大家有没有遇到这样的情况,比如视频合并时文件名没有按照正常顺序排列,像这样 可见,文件名排序是乱的.这个样子合并出来的视频一定也是乱的.所以得想办法把文件名修改一下,让软件读取出正确的顺序.闲话 ...
- 搜索 - 广度优先搜索(BFS)普通模板
bfs广度优先搜索模板 本人蒟蒻,为响应号召 写下bfs模板一篇 可以适用于求最短步数,等最优解问题.如有不足或者不对的地方请各位大佬及时指出 ^-^ 欢迎来戳 具体实现代码(C++) 各个模块功能和 ...
- wordcloud词云模块
wordcloud词云模块 下载 pip install wordcloud 使用 import wordcloud##调用整个模块 form wordcloud import WordCloud## ...
- USACO Roadblock
洛谷 P2176 [USACO14FEB]路障Roadblock 洛谷传送门 JDOJ 2406: USACO 2014 Feb Silver 2.Roadblock JDOJ传送门1 JDOJ 24 ...
- conda管理python环境
https://blog.csdn.net/wld914674505/article/details/80615761 source activate python36
- selenium数据读取模块
例如 数据保存在txt中 def info(path): web_info={} config = open(path) for line in config: result = [ele.strip ...
- 获取DOM元素的方式有哪些
document.getElementById();//id名 document.getElementsByTagName();//标签名 document.getElementsByClassNam ...
- 【BigData】Java基础_方法的定义与使用
1.概念 Java语言中的“方法”(Method)在其他语言当中也可能被称为“函数”(Function).对于一些复杂的代码逻辑,如果希望重复使用这些代码,并且做到“随时任意使用”,那么就可以将这些代 ...
- requests.session()会话保持
可能大家对session已经比较熟悉了,也大概了解了session的机制和原理,但是我们在做爬虫时如何会运用到session呢,就是接下来要讲到的会话保持. 首先说一下,为什么要进行会话保持的操作? ...
- 聊聊对称/非对称加密在HTTPS中的使用
目前常用的加密算法主要分成三类: 对称加密算法 非对称加密算法 消息摘要算法 在互联网中,信息防护主要涉及两个方面:信息窃取和信息篡改.对称/非对称加密算法能够避免信息窃取,而消息摘要算法能够避免信息 ...