C#深度学习のLINQ
一、LINQ的由来
LINQ是Language Integrated Query的缩写,意思是语言扩展查询
查询是一种从数据源检索数据的表达式。 查询通常用专门的查询语言来表示。 随着时间的推移,人们已经为各种数据源开发了不同的语言;例如,用于关系数据库的 SQL 和用于 XML 的 XQuery。 因此,开发人员不得不针对他们必须支持的每种数据源或数据格式而学习新的查询语言。 LINQ 通过提供一种跨各种数据源和数据格式使用数据的一致模型,简化了这一情况。 在 LINQ 查询中,始终会用到对象。 可以使用相同的基本编码模式来查询和转换 XML 文档、SQL 数据库、ADO.NET 数据集、.NET 集合中的数据以及对其有 LINQ 提供程序可用的任何其他格式的数据。
二、LINQ的用法
一个linq应用的步骤主要有:
1、符合要求的数据源(可枚举,实现或者隐式实现IEnumerable的数据源)
2、构建查询语句
3、查询
下面是一个具体例子:
//1、data source, 定义数据源
int[] score = new int[]
{
,
,
,
, };
// 2、numQuery is an IEnumerable<int> ,定义查询语句
var numQuery =
from num in score
where (num % ) ==
select num; //3、Query execution. 查询
foreach (int num in numQuery)
{
Console.Write("{0,1} ", num);
}
我们常见的foreach语句就是就是它的查询语句隐式
三、查询执行过程
linq查询我们foreach、.Count.Where.Any等是延迟执行的,也可以强制执行,需要用ToList,ToArry
List<int> numQuery2 =
(from num in numbers
where (num % ) ==
select num).ToList(); var numQuery3 =
(from num in numbers
where (num % ) ==
select num).ToArray();
四、常见示例
注:由于list和Arry等隐式实现IEnumerable接口,我们可以将对象放入其中来进行查询
1、多条件查询
IEnumerable<int> numQuery =
from num in score
where (num % ) == &&num <
select num; foreach (int num in numQuery)
{
Console.Write("{0,1} ", num);
}
2、结果排序
IEnumerable<int> numQuery =
from num in score
where (num % ) == &&num <
orderby num ascending
select num; foreach (int num in numQuery)
{
Console.Write("{0,1} ", num);
}
3、let语句
var studentQuery6 =
from student in students
let totalScore = student.Scores[] + student.Scores[] +
student.Scores[] + student.Scores[]
select totalScore; double averageScore = studentQuery6.Average();
Console.WriteLine("Class average score = {0}", averageScore);
4、group into
var studentQuery4 =
from student in students
group student by student.Last[] into studentGroup
orderby studentGroup.Key
select studentGroup; foreach (var groupOfStudents in studentQuery4)
{
Console.WriteLine(groupOfStudents.Key);
foreach (var student in groupOfStudents)
{
Console.WriteLine(" {0}, {1}",
student.Last, student.First);
}
}
5、select子句转换或投影
IEnumerable<string> studentQuery7 =
from student in students
where student.Last == "Garcia"
select student.First;
6、构建对象,select相当于返回
var studentQuery8 =
from student in students
let x = student.Scores[] + student.Scores[] +
student.Scores[] + student.Scores[]
where x > averageScore
select new { id = student.ID, score = x };
7、Join子句集合之间的连接,左连接,右连接。
var categoryQuery =
from cat in categories
join prod in products on cat equals prod.Category
select new { Category = cat, Name = prod.Name };
8、子查询
var queryGroupMax =
from student in students
group student by student.GradeLevel into studentGroup
select new
{
Level = studentGroup.Key,
HighestScore =
(from student2 in studentGroup
select student2.Scores.Average())
.Max()
};
9、通过Take()和Skip()实现只显示部分查询结果。
var qr = (from r in listStudents orderby r.score descending select r).Skip(i * pageSize).Take();
四、静态方法
System.Collections.Generic提供了一套 static (Shared 在 Visual Basic 中) 用于查询实现的对象方法 ,方便使用
主要有Aggregate、All、Any、Average、Contains、Count、Distinct、Except、First、GroupBy、GroupJoin、Join、Intersect、Last、Union、Where等
五、使用 yield 关键字,则指示在的方案、运算符或 get 访问器是迭代器。 使用的迭代器对集合的自定义迭代(语法糖)
static void Main()
{
// Display powers of 2 up to the exponent of 8:
foreach (int i in Power(, ))
{
Console.Write("{0} ", i);
}
} public static System.Collections.IEnumerable Power(int number, int exponent)
{
int result = ; for (int i = ; i < exponent; i++)
{
result = result * number;
yield return result;
}
}
public static class GalaxyClass
{
public static void ShowGalaxies()
{
var theGalaxies = new Galaxies();
foreach (Galaxy theGalaxy in theGalaxies.NextGalaxy)
{
Debug.WriteLine(theGalaxy.Name + " " + theGalaxy.MegaLightYears.ToString());
}
} public class Galaxies
{ public System.Collections.Generic.IEnumerable<Galaxy> NextGalaxy
{
get
{
yield return new Galaxy { Name = "Tadpole", MegaLightYears = };
yield return new Galaxy { Name = "Pinwheel", MegaLightYears = };
yield return new Galaxy { Name = "Milky Way", MegaLightYears = };
yield return new Galaxy { Name = "Andromeda", MegaLightYears = };
}
} } public class Galaxy
{
public String Name { get; set; }
public int MegaLightYears { get; set; }
}
}
欢迎qq加群学习:568055323
C#深度学习のLINQ的更多相关文章
- 【深度学习Deep Learning】资料大全
最近在学深度学习相关的东西,在网上搜集到了一些不错的资料,现在汇总一下: Free Online Books by Yoshua Bengio, Ian Goodfellow and Aaron C ...
- DeepMind背后的人工智能:深度学习原理初探
去年11月,一篇名为<Playing Atari with Deep Reinforcement Learning>的文章被初创人工智能公司DeepMind的员工上传到了arXiv网站.两 ...
- 【转】TensorFlow练习20: 使用深度学习破解字符验证码
验证码是根据随机字符生成一幅图片,然后在图片中加入干扰象素,用户必须手动填入,防止有人利用机器人自动批量注册.灌水.发垃圾广告等等 . 验证码的作用是验证用户是真人还是机器人:设计理念是对人友好,对机 ...
- 整理:深度学习 vs 机器学习 vs 模式识别
http://www.csdn.net/article/2015-03-24/2824301 近200篇机器学习&深度学习资料分享(含各种文档,视频,源码等) http://developer ...
- [深度学习大讲堂]从NNVM看2016年深度学习框架发展趋势
本文为微信公众号[深度学习大讲堂]特约稿,转载请注明出处 虚拟框架杀入 从发现问题到解决问题 半年前的这时候,暑假,我在SIAT MMLAB实习. 看着同事一会儿跑Torch,一会儿跑MXNet,一会 ...
- 手把手教你搭建深度学习平台——避坑安装theano+CUDA
python有多混乱我就不多说了.这个混论不仅是指整个python市场混乱,更混乱的还有python的各种附加依赖包.为了一劳永逸解决python的各种依赖包对深度学习造成的影响,本文中采用pytho ...
- 深度学习框架搭建之最新版Python及最新版numpy安装
这两天为了搭载深度学习的Python架构花了不少功夫,但是Theano对Python以及nunpy的版本都有限制,所以只能选用版本较新的python和nunpy以确保不过时.但是最新版Python和最 ...
- 机器学习&深度学习资料
机器学习(Machine Learning)&深度学习(Deep Learning)资料(Chapter 1) 机器学习(Machine Learning)&深度学习(Deep Lea ...
- 【转载】如何自学深度学习技术,大神Yann LeCun亲授建议
编者按:Quora 上有网友提问:自学机器学习技术,你有哪些建议?(What are your recommendations for self-studying machine learning), ...
随机推荐
- Android:剖析源码,随心所欲控制Toast显示
前言 Toast相信大家都不会陌生吧,如果对于Toast不甚了解,可以参考我的上一篇博客<Android:谈一谈安卓应用中的Toast情节>,里面有关于Toast基础比较详细的介绍.但是如 ...
- shell之最常用的服务脚本
任务需求:以最简单的方式管理 /usr/local/php7/sbin/php-fpm -c /usr/local/php7/etc/php.ini 这条命令 包括启动停止重启 使用技术:shell脚 ...
- 支持异步写入的日志类,支持Framework2.0
因为工作需要需要在XP上运行一个C#编写的Winform插件,我就用Framework2.0,因为存在接口交互所以想保留交易过程的入参出参. 考虑到插件本身实施的因素,就没有使用Log4.NLog等成 ...
- C# IQueryable和IEnumerable的区别
在使用EF查询数据的时候,我们常用的查询数据方式有linq to sql,linq to object, 查询返回的结果有两种类型:IQueryable.IEnumerable,两者内部的处理机制是完 ...
- 记一次servlet项目启动
前言 tomcat 和 jetty 都属于 web 容器. mac安装tomcat brew install tomcat 安装之后,输入 catalina -h,可以看到各种命令,如run.star ...
- Java学习笔记之——多线程
多线程编程 程序: 进程:一个程序运行就会产生一个进程 线程:进程的执行流程,一个进程至少有一个线程,称为主线程 如:QQ聊着天,同时在听音乐 一个进程可以有多个线程,多个线程共享同一个进程的资源 线 ...
- vue 新版本 webpack 代理 跨域设置
旧版本中:dev-server.js 这段去掉 var apiRoutes = express.Router() //getList apiRoutes.get('/getDiscList', fun ...
- HTML5 Video player jQuery plugin
<!DOCTYPE html> <html lang="en" > <head> <meta charset="utf-8&qu ...
- Promise原理讲解 && 实现一个Promise对象 (遵循Promise/A+规范)
1.什么是Promise? Promise是JS异步编程中的重要概念,异步抽象处理对象,是目前比较流行Javascript异步编程解决方案之一 2.对于几种常见异步编程方案 回调函数 事件监听 发布/ ...
- Get-CrmSetting返回Unable to connect to the remote server的解决办法
摘要: 微软动态CRM专家罗勇 ,回复302或者20190125可方便获取本文,同时可以在第一间得到我发布的最新博文信息,follow me!我的网站是 www.luoyong.me . 在Dynam ...