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), ...
随机推荐
- Shiro中的授权问题
在初识Shiro一文中,我们对Shiro的基本使用已经做了简单的介绍,不懂的小伙伴们可以先阅读上文,今天我们就来看看Shiro中的授权问题. Shiro中的授权,大体上可以分为两大类,一类是隐式角色, ...
- 动态代理的两种方式,以及区别(静态代理、JDK与CGLIB动态代理、AOP+IoC)
Spring学习总结(二)——静态代理.JDK与CGLIB动态代理.AOP+IoC 目录 一.为什么需要代理模式 二.静态代理 三.动态代理,使用JDK内置的Proxy实现 四.动态代理,使用cg ...
- RabbitMQ消息队列(八)-通过Topic主题模式分发消息(.Net Core版)
前两章我们讲了RabbitMQ的direct模式和fanout模式,本章介绍topic主题模式的应用.如果对direct模式下通过routingkey来匹配消息的模式已经有一定了解那fanout也很好 ...
- iOS逆向开发(8):微信自动添加好友
这一次,小程演示怎么让一个APP自动地运行,从而代替手工的操作.同样以"微信"以例,实现在一个微信群里面,对所有的成员,自动地一个一个地发出添加好友的请求. 知识点还是之前介绍的东 ...
- SpringBoot基础系列-使用日志
原创作品,可以转载,但是请标注出处地址:https://www.cnblogs.com/V1haoge/p/9996897.html SpringBoot基础系列-使用日志 概述 SpringBoot ...
- Docker快速搭建LNMP环境
一.使用Dockerfile制作镜像 前面的博客中已经介绍了如何基于容器制作镜像,此方法的原理是使用一个正在运行的容器,根据生产所需进行配置更改等操作后,使其满足生产环境,再将这个容器打包制作为镜像, ...
- 第18章 启动 - Identity Server 4 中文文档(v1.0.0)
IdentityServer是中间件和服务的组合.所有配置都在您的启动类中完成. 18.1 配置服务 您可以通过调用以下方法将IdentityServer服务添加到DI系统: public void ...
- chrome设置网页编码
新版的 chrome 没有这个设置选项,可以借助插件 charset 插件实现,到 web store 搜索安装即可.
- Java开发笔记(六十五)集合:HashSet和TreeSet
对于相同类型的一组数据,虽然Java已经提供了数组加以表达,但是数组的结构实在太简单了,第一它无法直接添加新元素,第二它只能按照线性排列,故而数组用于基本的操作倒还凑合,若要用于复杂的处理就无法胜任了 ...
- Eclipse Ctrl+Space 无法工作的问题
Window->preference->Keys 然后找到content Assist,然后重新设置为自己想要的Short cut 就好了