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), ...
随机推荐
- 前端笔记之HTML5&CSS3(上)新特性&音频视频&本地存储&自定义属性
一.HTML5简介 HTML 5 的第一份正式草案已于2008年1月22日公布.HTML5 仍处于完善之中.然而,大部分现代浏览器已经具备了某些 HTML5 支持. 2014年10月29日,万维网联盟 ...
- SpringBoot上传文件到本服务器 目录与jar包同级
前言 看标题好像很简单的样子,但是针对使用jar包发布SpringBoot项目就不一样了. 当你使用tomcat发布项目的时候,上传文件存放会变得非常简单,因为你可以随意操作项目路径下的资源.但是当你 ...
- Session执行机制与原理
Session执行机制与原理 作者:Stanley 罗昊 [转载请注明出处和署名,谢谢!] 什么是Session 首先了解一下Session的中文意思:一次会话,什么是一次会话呢?我举个例子:就我们现 ...
- Dockerfile 中的 COPY 与 ADD 命令
Dockerfile 中提供了两个非常相似的命令 COPY 和 ADD,本文尝试解释这两个命令的基本功能,以及其异同点,然后总结其各自适合的应用场景. Build 上下文的概念 在使用 docker ...
- [Vue] vue跳转外部链接
问题 vue 跳转外部链接问题,当跳转的时候会添加在当前地址后面 var url = 'www.baidu.com' //跳转1 window.localtion.href = url //跳转2 w ...
- Linux系统命令行整理
1.pwd 查看当前所在目录 2.cd / 跳往根目录 3.ls 查看当前目录所有子目录或文件 4.ls -l 列出当前目录详细信息 5.ls -lh h=human 人性化列出当前目录详细 ...
- WebClient下载文件
public void DownDile(string url) { WebClient client = new WebClient(); string URLAddress = @"ht ...
- Java_设计模式之享元模式
1.关于享元模式 享元模式有点类似于单例模式,都是只生成一个对象被共享使用.享元模式主要目的就是让多个对象实现共享,减少不会要额内存消耗,将多个对同一对象的访问集中起来,不必为每个访问者创建一个单独的 ...
- WEB前端 HTML
目录 WEB前端 HTML WEB前端 HTML TOC 什么是html? html的固有结构 注释 什么是标签? 标签分类 什么是标签属性? 适用于大多数HTML标签的属性 常用标签 常用引用标签 ...
- centos6.3部署配置LVS主从
LVS是Linux Virtual Server的简写,即Linux虚拟服务器,是一个虚拟的服务器集群系统.这个项目在1998年5月由章文嵩博士成立,是中国国内最早出现的自由软件项目之一.它的官方网址 ...