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接口自动化17-multipart/form-data表单提交
前言 multipart/form-data这种格式官方文档给的参考案例比较简单,实际情况中遇到会比较复杂,本篇讲解multipart/form-data的表单如何提交,非图片上传 禅道提交bug 1 ...
- android 8.0 悬浮窗 最简demo
MainActivity.java文件 package com.example.performance; import android.app.Activity; import android.con ...
- JDK9下载与安装
1.进入oracle官网下载页面 https://www.oracle.com/downloads/index.html 2.点击Menu 3.点击JAVA SE 4.点击JDK Download 5 ...
- NOIP 2008 笨小猴
洛谷 P1125 笨小猴 洛谷传送门 JDOJ 1539: [NOIP2008]笨小猴 T1 JDOJ传送门 Description 笨小猴的词汇量很小,所以每次做英语选择题的时候都很头疼.但是他找到 ...
- PAC、KNN和GridSearchCV
PCA PCA主要是用来数据降维,将高纬度的特征映射到低维度,具体可学习线性代数. 这里,我们使用sklearn中的PCA. from sklearn.decomposition import PCA ...
- Shell基础、输入输出重定向
1.Shell的功能: (1)Shell是命令解释器,把我们写的命令转化为内核能够识别的机器语言,然后内核调用硬件来完成相应的操作.操作完成后,内核操作结果返回给内核,Shell再将机器语言翻译为我们 ...
- c# HttpClient的HTTP/2支持
HTTP/2 是 HTTP 协议的主要修订版.HTTP/2 的一些显著功能是支持标头压缩和通过同一连接完全多路复用流.虽然 HTTP/2 保留了 HTTP 的语义(HTTP 标头.方法等),但它在数据 ...
- QThread 采用moveToThread方式实现多线程。 线程本身、connect关联的槽函数、connect关联的lambda对象分别运行在哪个线程中。
Qt如何实现多线程:https://www.cnblogs.com/azbane/p/11372531.html September 5,2019 先抛出几个问题,用问题来引导思维导向: 1.继承的Q ...
- 【java】使用jsp命令查看系统中java运行的程序及进程号
对于java独立运行的程序,他们在进程中的名字都是 Java(TM) Platform SE binary,如图 我们想知道这个进程运行的是哪个程序,怎么办呢? 答案是:可以在命令行下,运行:jps命 ...
- python 关于celery的异步任务队列的基本使用(celery+redis)【采用配置文件设置】
工程结构说明:源文件下载请访问https://i.cnblogs.com/Files.aspx __init__.py:实例化celery,并加载配置模块 celeryconfig.py:配置模块 t ...