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循环的更多相关文章

  1. (第一篇) 一步一步带你了解linq to Object

    要想学好linq to object 我们必须要先学习lambda 表达式,学习lambda 表达式呢我们必须了解匿名函数和匿名类及扩展方法,学习匿名函数,我们必须学会委托,这是本文的宗旨.下面开始第 ...

  2. while 循环,格式化输出和运算编码

    今日内容 1.while循环 while Ture:             content = input ("请输入你要喷的内容",输入Q退出)             if ...

  3. while循环、运算符和格式化输出以及编码

    一.while循环 1.while就是当的意思,while指当其后面的条件成立,就执行while下面的代码 写一段代码让程序从0打印到100的程序,每次循环+1. count = 0 while co ...

  4. 在net中json序列化与反序列化 面向对象六大原则 (第一篇) 一步一步带你了解linq to Object 10分钟浅谈泛型协变与逆变

    在net中json序列化与反序列化   准备好饮料,我们一起来玩玩JSON,什么是Json:一种数据表示形式,JSON:JavaScript Object Notation对象表示法 Json语法规则 ...

  5. while循环 运算符和编码

    昨日回顾 1. 初识python python是一门弱类型的解释型高级编程语言 解释器: CPython 官方提供的默认解释器. c语言实现的 PyPy 把python程序一次性进行编译. IPyth ...

  6. 04 Python之while循环/格式化输出/运算符/编码

    1. while循环 while 条件: 循环体(break,continue) else: 循环体(break,continue) break:彻底干掉一个循环,直接跳出. continue:停止当 ...

  7. 控制流程之if判断与while、for循环

    一.if判断 1.什么是if判断? 接收用户输入的名字 接受用户输入的密码 如果用户输入的名字=正确的名字 并且 用户输入的密码=正确的密码 告诉用户登录成功 否则, 告诉用户登录失败 2.为何要有i ...

  8. 整数分解、for循环阶乘

    整数分解 整数分解是什么呢??我们可以这样理解 我们写一个 3位数求出它的个位十位和百位 . 那么我们来写一个小的测试来看一下! public static void main(String[] ar ...

  9. 高性能日志类KLog(已开源代码)

    项目开源地址:https://github.com/ihambert/KLog  上回介绍了超简易日志类,但他有诸多的局限性,注定了不能作为一个网站的日志类. 那什么样的日志类才能用于网站呢.首先来假 ...

随机推荐

  1. 浅析Volatile关键字

    浅析Volatile关键字 在java中线程并发中,线程之间通信方式分为两种:共享内存和消息传递.共享内存指的是多个线程之间共享内存的属性状态:消息传递指的是线程之间发送信息来通信.在介绍volati ...

  2. 通过开启子进程的方式实现套接字服务端可以并发的处理多个链接以及通讯循环(用到了subprocess模块,解决粘包问题)

    今日作业:通过开启子进程的方式实现套接字服务端可以并发的处理多个链接以及通讯循环(用到了subprocess模块,解决粘包问题) server(服务端) import socket from mult ...

  3. centos7编译安装pgbouncer

    1.下载pgbouncer程序包和libevent依赖包 wget https://github.com/libevent/libevent/releases/download/release-2.1 ...

  4. 小学四则运算口算练习app---No.4

    今天主要是改了出题页中各个组件的位置以及时间的接收还有时间控制,代码如下:(但是存在一个问题  设置页面点击确定按钮进入出题界面时有时会闪退,未解决!) CalculatorActivity.clas ...

  5. html--前端javascript初识

    一.JavaScript简介 JavaScript是一种基于对象和事件驱动并具有安全性能的脚本语言,有了JavaScript,可使网页变得生动.使用它的目的是与HTML超文本标识语言.Java 脚本语 ...

  6. Random Access Iterator 徐州网络赛(树形dp)

    Random Access Iterator \[ Time Limit: 4000 ms \quad Memory Limit: 262144 kB \] 题意 给出伪代码,问按着伪代码在树上跑,能 ...

  7. git blame (10)

    git blame system_server.c 每一行提交的sha ,作者,提交的日期及提交的信息

  8. CLR Exception 0xE0434F4D和0xE0434352的区别

    <根据<CLR Exception---E0434352>和<CLR Exception---E0434F4D>这两篇随笔,我们会发现,这两个异常太相似了,除了代码值不一 ...

  9. Vue模板语法(一)

    Vue模板语法 一.插值 1.1.1 文本 {{msg}} 1.1.2 html 使用v-html指令用于输出html代码 1.1.3 属性 HTML属性中的值应使用v-bind指令 1.1.4 表达 ...

  10. Promise以及async和await的用法

    Promise是一个异步加载的方式,处理时使用new Promise返回一个对象,该对象可以调用then方法,then方法中有两个参数,第一个参数是加载成功时执行,第二个参数是加载失败时执行,then ...