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. Bootstrap基础学习 ---- 系列文章

    [Bootstrap基础学习]05 Bootstrap学习总结 [Bootstrap基础学习]04 Bootstrap的HTML和CSS编码规范 [Bootstrap基础学习]03 Bootstrap ...

  2. Spring——JDBC——数据库

    1.Spring 的数据访问哲学 数据访问的功能放到一个或者多个专注于此项任务的组件.这样的组件通常称为数据访问对象(data access object)DAO或者Repository. 为了避免应 ...

  3. serialize()和new FormData()的区别

    serialize()和FormData对象都可将表单数据序列化,后通过ajax异步提交 ,序列化表单就是将form表单中所有name属性序列化成KEY-VALUE的形式,提交到后台,后台以对象相应的 ...

  4. 常用dos命令(2)

    文件管理 type 显示文本文件的内容. copy 将一份或多份文件复制到另一个位置. del 删除一个或数个文件. move 移动文件并重命名文件和目录.(Windows XP Home Editi ...

  5. 题解 P4305 【[JLOI2011]不重复数字】

    来一波用vector的最短代码题解 P4305 [JLOI2011]不重复数字 关于hash表的部分大家可以看一看其他的题解,我就不说了 不定长数组vector的几个基本用法: 定义: vector& ...

  6. 解决——》java.lang.IllegalArgumentException: Body parameter 0 was null

    1.操作2.现象(错误信息)3.原因错误代码:4.解决1)方案一:@RequestBody(required=false)2)方案二:传参数时限制authSession不能为空ody paramete ...

  7. c3p0参数

    <c3p0-config> <default-config> <!--当连接池中的连接耗尽的时候c3p0一次同时获取的连接数.Default: 3 --> < ...

  8. Mybatis集成ehcache

    Mybatis集成ehcache 1.为什么需要缓存 拉高程序的性能 2. 什么样的数据需要缓存 很少被修改或根本不改的数据 业务场景比如:耗时较高的统计分析sql.电话账单查询sql等 3. ehc ...

  9. <虚树+树型DP> SDOI2011消耗战

    <虚树+树型DP> SDOI2011消耗战 #include <iostream> #include <cstdio> #include <cstring&g ...

  10. [RN] 可播放视频的播放器版本

    可播放视频的播放器版本 "react": "16.6.3", "react-native": "0.57.8", &qu ...