一、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的更多相关文章

  1. 【深度学习Deep Learning】资料大全

    最近在学深度学习相关的东西,在网上搜集到了一些不错的资料,现在汇总一下: Free Online Books  by Yoshua Bengio, Ian Goodfellow and Aaron C ...

  2. DeepMind背后的人工智能:深度学习原理初探

    去年11月,一篇名为<Playing Atari with Deep Reinforcement Learning>的文章被初创人工智能公司DeepMind的员工上传到了arXiv网站.两 ...

  3. 【转】TensorFlow练习20: 使用深度学习破解字符验证码

    验证码是根据随机字符生成一幅图片,然后在图片中加入干扰象素,用户必须手动填入,防止有人利用机器人自动批量注册.灌水.发垃圾广告等等 . 验证码的作用是验证用户是真人还是机器人:设计理念是对人友好,对机 ...

  4. 整理:深度学习 vs 机器学习 vs 模式识别

    http://www.csdn.net/article/2015-03-24/2824301 近200篇机器学习&深度学习资料分享(含各种文档,视频,源码等) http://developer ...

  5. [深度学习大讲堂]从NNVM看2016年深度学习框架发展趋势

    本文为微信公众号[深度学习大讲堂]特约稿,转载请注明出处 虚拟框架杀入 从发现问题到解决问题 半年前的这时候,暑假,我在SIAT MMLAB实习. 看着同事一会儿跑Torch,一会儿跑MXNet,一会 ...

  6. 手把手教你搭建深度学习平台——避坑安装theano+CUDA

    python有多混乱我就不多说了.这个混论不仅是指整个python市场混乱,更混乱的还有python的各种附加依赖包.为了一劳永逸解决python的各种依赖包对深度学习造成的影响,本文中采用pytho ...

  7. 深度学习框架搭建之最新版Python及最新版numpy安装

    这两天为了搭载深度学习的Python架构花了不少功夫,但是Theano对Python以及nunpy的版本都有限制,所以只能选用版本较新的python和nunpy以确保不过时.但是最新版Python和最 ...

  8. 机器学习&深度学习资料

    机器学习(Machine Learning)&深度学习(Deep Learning)资料(Chapter 1) 机器学习(Machine Learning)&深度学习(Deep Lea ...

  9. 【转载】如何自学深度学习技术,大神Yann LeCun亲授建议

    编者按:Quora 上有网友提问:自学机器学习技术,你有哪些建议?(What are your recommendations for self-studying machine learning), ...

随机推荐

  1. 初学Java Web(6)——JSP学习总结

    为什么要学习 JSP Servlet 的短板: Servlet 的出现,是为了解决动态输出网页的问题. 虽然这样做目的能达到,但是存在一些缺陷: 在 Servlet 输出网页片段非常恶心 (可读性差, ...

  2. RabbitMQ消息队列(二)-RabbitMQ消息队列架构与基本概念

    没错我还是没有讲怎么安装和写一个HelloWord,不过快了,这一章我们先了解下RabbitMQ的基本概念. RabbitMQ架构 说是架构其实更像是应用场景下的架构(自己画的有点丑,勿嫌弃) 从图中 ...

  3. 【c#】RabbitMQ学习文档(一)Hello World

    一.简介 RabbitMQ是一个消息的代理器,用于接收和发送消息,你可以这样想,他就是一个邮局,当您把需要寄送的邮件投递到邮筒之时,你可以确定的是邮递员先生肯定会把邮件发送到需要接收邮件的人的手里,不 ...

  4. 使用yum安装不知道到底安装在什么文件夹

    find /* >yum001    #记录之前的文件夹 find /* >yum002    #记录安装完成后的文件夹 diff yum001 yum002 >yum000     ...

  5. man帮助文档打印

    这里不讨论大家都知道的man重定向的一般常用方法(col处理方法)$ man find | col -b > man_fine.txt [跟着我的思路走]假如您像我一样,直接使用如下命令导出fi ...

  6. C#數據庫

    一.連接數據庫 1.定義連接數據庫的字符串 string source = "server=(local); integrated security=SSPI; database=test& ...

  7. [日常] nginx的错误日志error_log设置

    nginx error_log设置1.error_log syslog:server=192.168.1.1 [级别] //直接发送给远程syslog日志集中服务器2.error_log stderr ...

  8. 【java】随机生成6位的数字

    int radomInt = new Random().nextInt(999999); int radomInt2 =(int)((Math.random()*9+1)*100000); Syste ...

  9. 设计模式总结(Java)—— 观察者模式

    概述 它用于建立一种对象与对象之间的依赖关系,一个对象发生改变时将自动通知其他对象,其他对象将相应作出反应.在观察者模式中,发生改变的对象称为观察目标,而被通知的对象称为观察者,一个观察目标可以对应多 ...

  10. java开发环境配置——IntelliJ IDEA

    关于开发工具,之前是用eclipse,后来用了一段时间idea后,发现idea比eclipse好用太多了,所以推荐大家用idea 官网下载地址:https://www.jetbrains.com/id ...