Liqn基础
var 临时数据= from 临时变量 in 集合对象或数据库对象
where 条件表达式
order by条件
select 临时变量中被查询的值
group by 条件
linq 查询与 Lamda表达式查询比对
public static IList<Racer> GetChampions()
{
if (racers == null)
{
racers = new List<Racer>();
racers.Add(new Racer("Nino", "Farina", "Italy", , , new int[] { }, new string[] { "Alfa Romeo" }));
racers.Add(new Racer("Alberto", "Ascari", "Italy", , , new int[] { , }, new string[] { "Ferrari" }));
racers.Add(new Racer("Juan Manuel", "Fangio", "Argentina", , , new int[] { , , , , }, new string[] { "Alfa Romeo", "Maserati", "Mercedes", "Ferrari" }));
racers.Add(new Racer("Mike", "Hawthorn", "UK", , , new int[] { }, new string[] { "Ferrari" }));
racers.Add(new Racer("Phil", "Hill", "USA", , , new int[] { }, new string[] { "Ferrari" }));
racers.Add(new Racer("John", "Surtees", "UK", , , new int[] { }, new string[] { "Ferrari" }));
racers.Add(new Racer("Jim", "Clark", "UK", , , new int[] { , }, new string[] { "Lotus" }));
racers.Add(new Racer("Jack", "Brabham", "Australia", , , new int[] { , , }, new string[] { "Cooper", "Brabham" }));
racers.Add(new Racer("Denny", "Hulme", "New Zealand", , , new int[] { }, new string[] { "Brabham" }));
racers.Add(new Racer("Graham", "Hill", "UK", , , new int[] { , }, new string[] { "BRM", "Lotus" }));
racers.Add(new Racer("Jochen", "Rindt", "Austria", , , new int[] { }, new string[] { "Lotus" }));
racers.Add(new Racer("Jackie", "Stewart", "UK", , , new int[] { , , }, new string[] { "Matra", "Tyrrell" }));
racers.Add(new Racer("Emerson", "Fittipaldi", "Brazil", , , new int[] { , }, new string[] { "Lotus", "McLaren" }));
racers.Add(new Racer("James", "Hunt", "UK", , , new int[] { }, new string[] { "McLaren" }));
racers.Add(new Racer("Mario", "Andretti", "USA", , , new int[] { }, new string[] { "Lotus" }));
racers.Add(new Racer("Jody", "Scheckter", "South Africa", , , new int[] { }, new string[] { "Ferrari" }));
racers.Add(new Racer("Alan", "Jones", "Australia", , , new int[] { }, new string[] { "Williams" }));
racers.Add(new Racer("Keke", "Rosberg", "Finland", , , new int[] { }, new string[] { "Williams" }));
racers.Add(new Racer("Niki", "Lauda", "Austria", , , new int[] { , , }, new string[] { "Ferrari", "McLaren" }));
racers.Add(new Racer("Nelson", "Piquet", "Brazil", , , new int[] { , , }, new string[] { "Brabham", "Williams" }));
racers.Add(new Racer("Ayrton", "Senna", "Brazil", , , new int[] { , , }, new string[] { "McLaren" }));
racers.Add(new Racer("Nigel", "Mansell", "UK", , , new int[] { }, new string[] { "Williams" }));
racers.Add(new Racer("Alain", "Prost", "France", , , new int[] { , , , }, new string[] { "McLaren", "Williams" }));
racers.Add(new Racer("Damon", "Hill", "UK", , , new int[] { }, new string[] { "Williams" }));
racers.Add(new Racer("Jacques", "Villeneuve", "Canada", , , new int[] { }, new string[] { "Williams" }));
racers.Add(new Racer("Mika", "Hakkinen", "Finland", , , new int[] { , }, new string[] { "McLaren" }));
racers.Add(new Racer("Michael", "Schumacher", "Germany", , , new int[] { , , , , , , }, new string[] { "Benetton", "Ferrari" }));
racers.Add(new Racer("Fernando", "Alonso", "Spain", , , new int[] { , }, new string[] { "Renault" }));
racers.Add(new Racer("Kimi", "Räikkönen", "Finland", , , new int[] { }, new string[] { "Ferrari" }));
racers.Add(new Racer("Lewis", "Hamilton", "UK", , , new int[] { }, new string[] { "McLaren" }));
racers.Add(new Racer("Jenson", "Button", "UK", , , new int[] { }, new string[] { "Brawn GP" }));
racers.Add(new Racer("Sebastian", "Vettel", "Germany", , , new int[] { , }, new string[] { "Red Bull Racing" }));
}
return racers;
}
var query = from r in Formula1.GetChampions()
where r.Country == "Brazil"
orderby r.Wins descending
select r; foreach (Racer r in query)
{
Console.WriteLine("{0:A}", r);
} // Lamda表达式查询
IList<Racer> list = Formula1.GetChampions().Where(r => r.Country == "Brazil").OrderBy(a => a.Wins).ToList(); foreach (Racer r in list)
{
Console.WriteLine("{0:A}", r);
}
Formula1.GetChampions()是个集合对象。我们这边用了lamda表达式也可以很快的查询 。from、where、orderby、 descending和select都是这个查询中的预定义关键字,查询语句必须以form开头 以select 或者 group 子句结尾。变量query 指定了linq 查询,该语句不是通过赋值语句查询的,只要使用foreach循环方法
var可以将查询结果的类型显式声明为 IEnumerable<string>。 但是,在第二个表达式中必须使用 var,因为结果是一个匿名类型集合,而该类型的名称只有编译器本身可以访问。
编译器会转换linq查询,但是调用方法而不是linq查询。这里的Where是个扩展方法。
Liqn基础的更多相关文章
- C#LIQN基础知识
- java基础集合经典训练题
第一题:要求产生10个随机的字符串,每一个字符串互相不重复,每一个字符串中组成的字符(a-zA-Z0-9)也不相同,每个字符串长度为10; 分析:*1.看到这个题目,或许你脑海中会想到很多方法,比如判 ...
- node-webkit 环境搭建与基础demo
首先去github上面下载(地址),具体更具自己的系统,我的是windows,这里只给出windows的做法 下载windows x64版本 下载之后解压,得到以下东西 为了方便,我们直接在这个目录中 ...
- js学习笔记:webpack基础入门(一)
之前听说过webpack,今天想正式的接触一下,先跟着webpack的官方用户指南走: 在这里有: 如何安装webpack 如何使用webpack 如何使用loader 如何使用webpack的开发者 ...
- Golang, 以17个简短代码片段,切底弄懂 channel 基础
(原创出处为本博客:http://www.cnblogs.com/linguanh/) 前序: 因为打算自己搞个基于Golang的IM服务器,所以复习了下之前一直没怎么使用的协程.管道等高并发编程知识 ...
- [C#] C# 基础回顾 - 匿名方法
C# 基础回顾 - 匿名方法 目录 简介 匿名方法的参数使用范围 委托示例 简介 在 C# 2.0 之前的版本中,我们创建委托的唯一形式 -- 命名方法. 而 C# 2.0 -- 引进了匿名方法,在 ...
- HTTPS 互联网世界的安全基础
近一年公司在努力推进全站的 HTTPS 化,作为负责应用系统的我们,在配合这个趋势的过程中,顺便也就想去搞清楚 HTTP 后面的这个 S 到底是个什么含义?有什么作用?带来了哪些影响?毕竟以前也就只是 ...
- Swift与C#的基础语法比较
背景: 这两天不小心看了一下Swift的基础语法,感觉既然看了,还是写一下笔记,留个痕迹~ 总体而言,感觉Swift是一种前后端多种语言混合的产物~~~ 做为一名.NET阵营人士,少少多多总喜欢通过对 ...
- .NetCore MVC中的路由(1)路由配置基础
.NetCore MVC中的路由(1)路由配置基础 0x00 路由在MVC中起到的作用 前段时间一直忙于别的事情,终于搞定了继续学习.NetCore.这次学习的主题是MVC中的路由.路由是所有MVC框 ...
随机推荐
- 使用PHP解压文件Unzip
这是一个非常方便的PHP函数从.zip文件解压缩文件.它有两个参数:第一个是压缩文件的路径和第二 function unzip_file($file, $destination) { // creat ...
- [转载]css3属性box-sizing:border-box的作用
http://jastate.com/css3-properties-box-sizingborder-box.html 定义和用法 按照w3c school的说法,box-sizing:border ...
- BZOJ 2115: [Wc2011] Xor
2115: [Wc2011] Xor Time Limit: 10 Sec Memory Limit: 259 MB Submit: 2794 Solved: 1184 [Submit][Stat ...
- swift中的&-备
参数的传递引用 类是引用类型,其他的数据类型如整型.浮点型.布尔型.字符.字符串.元组.集合.枚举和结构体全部是值类型. 有的时候就是要将一个值类型参数以引用方式传递,这也是可以实现的,Swift提供 ...
- iOS知识点全梳理-备用
感谢大神分享 文/Jack_lin(简书作者)原文链接:http://www.jianshu.com/p/5d2163640e26著作权归作者所有,转载请联系作者获得授权,并标注“简书作者”. 序言 ...
- windows批处理命令教程
批处理文件是无格式的文本文件,它包含一条或多条命令.它的文件扩展名为 .bat 或 .cmd.在命令提示下键入批处理文件的名称,或者双击该批处理文件,系统就会调用Cmd.exe按照该文件中各个命令 ...
- Linux Shell逻辑运算符和表达式详解
Shell 逻辑运算符涉及以下几种类型,只要适当选择,可以解决我们很多复杂的判断,达到事半功倍效果. 一.逻辑判断1.关于文件与目录的逻辑判断-f 常用.判断『文件』是否为普通文件,比如: if [ ...
- js全局函数
http://www.w3cschool.cc/jsref/jsref-obj-global.html 以前没搞懂JS的全局函数,全局函数和window对象的函数不一样.全局函数不属于任何一个内置对象 ...
- 一位搬家师傅的O2O之旅
一位搬家师傅的O2O之旅 By 诸神之黄昏 | 2014/08/14 [核心提示] 一位普通的搬家师傅,无意中被卷入如火如荼的 O2O 浪潮,起初,互联网让他尝到了甜头,后来则是更多的困惑和不解. 再 ...
- Dynamic Binding & Static Binding
Reference: JavaPoint BeginnerBook What is Binding Connecting a method call to the method body is kno ...