LINQ使用与并行
LINQ介绍
參考:https://msdn.microsoft.com/en-us/library/bb397906.aspx
LINQ查询主要运行操作包含:1)获取数据源;2)创建查询;3)运行查询。须要注意的是仅仅有在使用查询结果的时候才会去运行查询,或者在创建查询之后再加上tolist或者toarray之类的功能则能够马上运行。
// The Three Parts of a LINQ Query:
// 1. Data source.
int[] numbers = new int[7] { 0, 1, 2, 3, 4, 5, 6 };
// 2. Query creation.
// numQuery is an IEnumerable<int>
var numQuery =
from num in numbers
where (num % 2) == 0
select num;
// 3. Query execution.
foreach (int num in numQuery)
{
Console.Write("{0,1} ", num);
}
数据源能够来自于Array、List等或者直接从文件里读取的结果。
查询语句操作
參考:https://msdn.microsoft.com/en-us/library/bb397927.aspx
Filter: where cust.City==”London” && cust.Name == “Devon”
Order: orderby cust.Name ascending
Group: group cust by cust.City into custGroup where custGroup.Count() > 2
Join: join dist in distributors on cust.City equals dist.City
其它功能
我们在使用LINQ操作的时候,往往另一些额外的需求,比方去重啊。分组啊等等。
// The Three Parts of a LINQ Query:
// 1. Data source.
int[] numbers = new int[] { 0, 1, 2, 3, 4, 5, 6, 4 };
// 2. Query creation.
// numQuery is an IEnumerable<int>
var numQuery =
from num in numbers
where (num % 2) == 0
select num;
// 3. Query execution.
foreach (int num in numQuery.Distinct())
{
Console.WriteLine("{0,1} ", num);
}
foreach (var num in numQuery.GroupBy(key => key))
{
Console.WriteLine("{0}:{1} ", num.Key, num.Count());
}
foreach (int num in numQuery.GroupBy(key => key).Select(key => key.Key))
{
Console.WriteLine("{0,1} ", num);
}
并行处理
LINQ比較强大的是还提供了可并行处理的查询。这使得我们能够借助它来完毕一些查询或者处理的并行操作。
static void Main(string[] args)
{
// The Three Parts of a LINQ Query:
// 1. Data source.
int[] numbers = new int[100];
for (var i = 0; i < 100; i++)
numbers[i] = i;
// 2. Query creation.
// numQuery is an IEnumerable<int>
var numQuery =
from num in numbers
where (num % 2) == 0
select num;
// 3. Query execution.
Split(numQuery.ToList(), 10)
.AsParallel()
.WithDegreeOfParallelism(3)
.Select(process)
.ToList();
Console.ReadKey();
}
static Tuple<List<int>, int> process(Tuple<List<int>, int> input)
{
foreach (var num in input.Item1)
Console.WriteLine("{0}-{1}", num, input.Item2);
return input;
}
static IEnumerable<Tuple<List<int>, int>> Split(List<int> nums, int Count)
{
int index = 0;
List<int> num = new List<int>();
foreach(var nm in nums)
{
num.Add(nm);
if (num.Count >= Count)
{
yield return new Tuple<List<int>, int>(num, index++);
num = new List<int>();
}
}
if(num.Count > 0)
{
yield return new Tuple<List<int>, int>(num, index++);
}
}
具体介绍能够參考:https://msdn.microsoft.com/en-us/library/dd997425(v=vs.110).aspx
LINQ使用与并行的更多相关文章
- [LINQ2Dapper]最完整Dapper To Linq框架(四)---Linq和SQL并行使用
目录 [LINQ2Dapper]最完整Dapper To Linq框架(一)---基础查询 [LINQ2Dapper]最完整Dapper To Linq框架(二)---动态化查询 [LINQ2Dapp ...
- 并行Linq
有时候我们对大批量数据进行处理,此时并行linq就起作用了. 并行查询 对于以下查询可以耗时会非常大,如下: ; var r = new Random(); , arraySize).Select(x ...
- .NET 实现并行的几种方式(三)
本随笔续接:.NET 实现并行的几种方式(二) 在前两篇随笔中,先后介绍了 Thread .ThreadPool .IAsyncResult (即 APM系列) .Task .TPL (Task Pa ...
- .NET并行编程实践(一:.NET并行计算基本介绍、并行循环使用模式)
阅读目录: 1.开篇介绍 2.NET并行计算基本介绍 3.并行循环使用模式 3.1并行For循环 3.2并行ForEach循环 3.3并行LINQ(PLINQ) 1]开篇介绍 最近这几天在捣鼓并行计算 ...
- c# Linq查询
c#提供的ling查询极大的遍历了集合的查询过程,且使用简单方便,非常的有用. 下面将分别用简单的例子说明:ling基本查询.延迟查询属性.类型筛选.复合from字句.多级排序.分组查询.联合查询.合 ...
- 5天玩转C#并行和多线程编程 —— 第二天 并行集合和PLinq
5天玩转C#并行和多线程编程系列文章目录 5天玩转C#并行和多线程编程 —— 第一天 认识Parallel 5天玩转C#并行和多线程编程 —— 第二天 并行集合和PLinq 5天玩转C#并行和多线程编 ...
- .net LINQ and PLINQ
本文 学习自 微软官网文档 2016/12 LINQ 背景 以前写与DB 相关的代码, 程序员须要懂开发语言(C#, VB)和查询语言跟数据库交互. LINQ 的出现使应用程序形成基于集合 ...
- 并行编程之PLINQ
并行编程之PLINQ 并行 LINQ (PLINQ) 是 LINQ 模式的并行实现.PLINQ 的主要用途是通过在多核计算机上以并行方式执行查询委托来加快 LINQ to Objects 查询的执行速 ...
- .NET并行计算基本介绍、并行循环使用模式
.NET并行计算基本介绍.并行循环使用模式) 阅读目录: 1.开篇介绍 2.NET并行计算基本介绍 3.并行循环使用模式 3.1并行For循环 3.2并行ForEach循环 3.3并行LINQ(PLI ...
随机推荐
- android 使用 LocalStorage
PS:本身是.net 出身 因为项目需要 研究了好几天安卓 这个方法网上也有 自己也写出来 有时间自己看看 和 给还没有解决问题的朋友借鉴下,下面有标个重点 是允许使用localstorage 的关键 ...
- MSSQL删除重复记录
SQL(根据自己需要改列名.表名): delete from tableA where id not in (select min(id) from tableA group by name,age)
- thinkphp函数学习(3): C函数详解
function C($name=null, $value=null,$default=null) { static $_config = array(); // 无参数时获取所有 if (empty ...
- 51nod 1101 换零钱 【完全背包变形/无限件可取】
1101 换零钱 基准时间限制:1 秒 空间限制:131072 KB 分值: 20 难度:3级算法题 收藏 关注 N元钱换为零钱,有多少不同的换法?币值包括1 2 5分,1 2 5角,1 2 5 ...
- Python与数据库[1] -> 数据库接口/DB-API[3] -> ODBC 适配器
ODBC适配器 / ODBC Adaptor ODBC(Open Database Connectivity,开放数据库互连)是微软公司开放服务结构(WOSA,Windows Open Service ...
- Babel的配置和使用
自从 Babel 由版本5升级到版本6后,在安装和使用方式上与之前大相径庭,于是写了这篇入坑须知,以免被新版本所坑. 坑一:本地安装和全局安装 全局安装只需: $ npm install --glob ...
- linux命令行翻页
在linux上面执行命令,若命令太多屏幕显示不完,通过Shift+pageup/pageDown来查看. putty连接linux后执行就不存在这个问题.
- delphi crc校验函数
function CalCRC16(AData: array of Byte; AStart, AEnd: Integer): string;const GENP=$8408; //多项式公式X1 ...
- tiny4412 串口驱动分析八 --- log打印的几个阶段之内核启动阶段(printk tiny4412串口驱动的注册)
作者:彭东林 邮箱:pengdonglin137@163.com 开发板:tiny4412ADK+S700 4GB Flash 主机:Wind7 64位 虚拟机:Vmware+Ubuntu12_04 ...
- NSOperationQueue 和 NSOperation
The NSOperationQueue class regulates the execution of a set of NSOperation objects. After being adde ...