ASP.NET---八大子句
LINQ的基本语法包含如下的8个上下文关键字,这些关键字和具体的说明如下:
关键字 |
说明 |
from |
指定范围变量和数据源 |
where |
根据bool表达式从数据源中筛选数据 |
select |
指定查询结果中的元素所具有的类型或表现形式 |
group |
对查询结果按照键值进行分组(IGrouping<TKey,TElement>) |
into |
提供一个标识符,它可以充当对join、group或select子句结果的引用 |
orderby |
对查询出的元素进行排序(ascending/descending) |
join |
按照两个指定匹配条件来Equals连接两个数据源 |
let |
产生一个用于存储查询表达式中的子表达式查询结果的范围变量 |
1.from子句
class CompoundFrom
{// The element type of the data source.public class Student{public string LastName { get; set; }public List<int> Scores {get; set;}}static void Main(){// Use a collection initializer to create the data source. Note that // each element in the list contains an inner sequence of scores.List<Student> students = new List<Student>{new Student {LastName="Omelchenko", Scores= new List<int> {97, 72, 81, 60}},new Student {LastName="O'Donnell", Scores= new List<int> {75, 84, 91, 39}},new Student {LastName="Mortensen", Scores= new List<int> {88, 94, 65, 85}},new Student {LastName="Garcia", Scores= new List<int> {97, 89, 85, 82}},new Student {LastName="Beebe", Scores= new List<int> {35, 72, 91, 70}} }; // Use a compound from to access the inner sequence within each element.// Note the similarity to a nested foreach statement.var scoreQuery = from student in studentsfrom score in student.Scoreswhere score > 90select new { Last = student.LastName, score };// Execute the queries.Console.WriteLine("scoreQuery:");// Rest the mouse pointer on scoreQuery in the following line to // see its type. The type is IEnumerable<'a>, where 'a is an // anonymous type defined as new {string Last, int score}. That is,// each instance of this anonymous type has two members, a string // (Last) and an int (score).foreach (var student in scoreQuery){Console.WriteLine("{0} Score: {1}", student.Last, student.score);}// Keep the console window open in debug mode.Console.WriteLine("Press any key to exit.");Console.ReadKey();}
}
/*
scoreQuery:
Omelchenko Score: 97
O'Donnell Score: 91
Mortensen Score: 94
Garcia Score: 97
Beebe Score: 91
*/
2.where子句
where子句用在查询表达式中,用于指定将在查询表达式中返回数据源中的哪些元素。 它将一个布尔条件(谓词)应用于每
个源元素(由范围变量引用),并返回满足指定条件的元素。 一个查询表达式可以包含多个 where子句,一个子句可以包含多
个谓词子表达式。
例:
where子句筛选出除小于五的数字外的所有数字。
class WhereSample
{static void Main(){ // Simple data source. Arrays support IEnumerable<T>.int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 };// Simple query with one predicate in where clause.var queryLowNums =from num in numberswhere num < 5select num;// Execute the query.foreach (var s in queryLowNums){Console.Write(s.ToString() + " ");}}
}
//Output: 4 1 3 2 0
注:where子句是一种筛选机制。 除了不能是第一个或最后一个子句外,它几乎可以放在查询表达式中的任何位置。
3.select子句
例:
class SelectSample1
{ static void Main(){ //Create the data sourceList<int> Scores = new List<int>() { 97, 92, 81, 60 };// Create the query.IEnumerable<int> queryHighScores =from score in Scoreswhere score > 80select score;// Execute the query.foreach (int i in queryHighScores){Console.Write(i + " ");} }
}
//Output: 97 92 81
4.group子句
可以用group子句结束查询表达式
例:
// Query variable is an IEnumerable<IGrouping<char, Student>>
var studentQuery1 =from student in studentsgroup student by student.Last[0];
5.into子句
class IntoSample1
{static void Main(){// Create a data source.string[] words = { "apples", "blueberries", "oranges", "bananas", "apricots"};// Create the query.var wordGroups1 =from w in wordsgroup w by w[0] into fruitGroupwhere fruitGroup.Count() >= 2select new { FirstLetter = fruitGroup.Key, Words = fruitGroup.Count() };// Execute the query. Note that we only iterate over the groups, // not the items in each groupforeach (var item in wordGroups1){Console.WriteLine(" {0} has {1} elements.", item.FirstLetter, item.Words);}// Keep the console window open in debug modeConsole.WriteLine("Press any key to exit.");Console.ReadKey();}
}
/* Output:a has 2 elements.b has 2 elements.
*/
仅当希望对每个组执行附加查询操作时,才需在 group子句中使用 into。
6.orderby子句
例:第一个查询按字母顺序从 A 开始对字词排序,而第二个查询则按降序对相同的字词排序。
class OrderbySample1
{static void Main(){ // Create a delicious data source.string[] fruits = { "cherry", "apple", "blueberry" };// Query for ascending sort.IEnumerable<string> sortAscendingQuery =from fruit in fruitsorderby fruit //"ascending" is defaultselect fruit;// Query for descending sort.IEnumerable<string> sortDescendingQuery =from w in fruitsorderby w descendingselect w; // Execute the query.Console.WriteLine("Ascending:");foreach (string s in sortAscendingQuery){Console.WriteLine(s);}// Execute the query.Console.WriteLine(Environment.NewLine + "Descending:");foreach (string s in sortDescendingQuery){Console.WriteLine(s);}// Keep the console window open in debug mode.Console.WriteLine("Press any key to exit.");Console.ReadKey();}
}
/* Output:
Ascending:
apple
blueberry
cherryDescending:
cherry
blueberry
apple
*/
7.join子句
join子句将 2 个源序列作为输入。 每个序列中的元素都必须是可以与其他序列中的相应属性进行比较的属性,
或者包含一个这样的属性。 join子句使用特殊 equals 关键字比较指定的键是否相等。 join子句执行的所有联接
都是同等联接。 join子句的输出形式取决于执行的联接的具体类型。 以下是 3 种最常见的联接类型:
内部联接
分组联接
左外部联接
内部联接
var innerJoinQuery =from category in categoriesjoin prod in products on category.ID equals prod.CategoryIDselect new { ProductName = prod.Name, Category = category.Name }; //produces flat sequence
分组联接
var innerGroupJoinQuery =from category in categoriesjoin prod in products on category.ID equals prod.CategoryID into prodGroupselect new { CategoryName = category.Name, Products = prodGroup };
左外部联接
var leftOuterJoinQuery =from category in categoriesjoin prod in products on category.ID equals prod.CategoryID into prodGroupfrom item in prodGroup.DefaultIfEmpty(new Product { Name = String.Empty, CategoryID = 0 })select new { CatName = category.Name, ProdName = item.Name };
8.let子句
在查询表达式中,存储子表达式的结果有时很有帮助,可在后续子句中使用。 可以通过 let关键字执行此操作,
该关键字创建一个新的范围变量并通过提供的表达式结果初始化该变量。 使用值进行初始化后,范围变量不能
用于存储另一个值。 但是,如果范围变量持有可查询类型,则可以查询该变量。
以两种方式使用以下示例 let:
创建一个可以查询其自身的可枚举类型。
使查询仅调用一次范围变量 word上的 ToLower。 如果不使用 let,则不得不调用 where子句中的每个谓词的 ToLower。
class LetSample1
{static void Main(){string[] strings = {"A penny saved is a penny earned.","The early bird catches the worm.","The pen is mightier than the sword." };// Split the sentence into an array of words// and select those whose first letter is a vowel.var earlyBirdQuery =from sentence in stringslet words = sentence.Split(' ')from word in wordslet w = word.ToLower()where w[0] == 'a' || w[0] == 'e'|| w[0] == 'i' || w[0] == 'o'|| w[0] == 'u'select word;// Execute the query.foreach (var v in earlyBirdQuery){Console.WriteLine("\"{0}\" starts with a vowel", v);}// Keep the console window open in debug mode.Console.WriteLine("Press any key to exit.");Console.ReadKey();}
}
/* Output:"A" starts with a vowel"is" starts with a vowel"a" starts with a vowel"earned." starts with a vowel"early" starts with a vowel"is" starts with a vowel
*/
ASP.NET---八大子句相关推荐
- ASP.NET网站开发——LINQ TO SQL 查询数据库数据(八大子句)
LINQ查询字句概述 1.查询(Query)是一组指令,这些指令可以从一个或多个给定的数据源中检索数据,并指定检索结果的数据类型和表现形式. 2.查询表达式是一种用查询语法表示的表达式,由一组用类似于 ...
- Linq to SQL八大子句
查询数据库中的数据 from- in子句 指定查询操作的数据源和范围变量 select子句 指定查询结果的类型和表现形式 where子句 筛选元素的逻辑条件,一般由逻辑运算符组成 group- by子 ...
- LINQ查询————八大基本子句
八大语句: 1.from ... in子句:制定查询操作的数据源和范围变量. 2.select子句:指定查询结果的类型和表现形式. 3.where子句:筛选元素的逻辑条件,一般由逻辑运算符组成. 4. ...
- LINQ篇:ASP.NET using LINQ(Part One) Scott大师的产物
[原文地址]Using LINQ with ASP.NET (Part 1) [原文发表日期]Sunday, May 14, 2006 9:49 PM 最近使我激动不已的新鲜事之一就是LINQ系列技术 ...
- ASP.NET 面试题和答案(不断更新)
1. 简述 private. protected. public.Friend. internal 修饰符的访问权限. 答 . private : 私有成员, 在类的内部才可以访问(只能从其声明上下文 ...
- Asp.net 内置对象
(1)简述ASP.NET内置对象. 答:ASP.NET提供了内置对象有Page.Request.Response.Application.Session.Server.Mail和Cookies.这些对 ...
- 提高 ASP.NET Web 应用性能的 24 种方法和技巧
那性能问题到底该如何解决?以下是应用系统发布前,作为 .NET 开发人员需要检查的点. 1.debug=「false」 当创建 ASP.NET Web应用程序,默认设置为「true」.开发过程中,设置 ...
- Scott Mitchell 的ASP.NET 2.0数据教程之一: 创建一个数据访问层
原文 | 下载本教程中的编码例子 | 下载本教程的英文PDF版 导言 作为web开发人员,我们的生活围绕着数据操作.我们建立数据库来存储数据,写编码来访问和修改数据,设计网页来采集和汇总数据.本文是研 ...
- 创建用于 ASP.NET 的分页程序控件
Dino Esposito Wintellect 2003 年 10 月 适用于: Microsoft® ASP.NET 摘要:解决向任何 ASP.NET 控件添加分页功能的问题.还为开发复合 ...
最新文章
- echarts 网格属性_设置echarts_的网格样式颜色
- 包教包会!7段代码带你玩转Python条件语句(附代码)
- #include algorithm 常用函数
- python中怎么输出中文-python中使用print输出中文的方法
- ASP.NET MVC2+MSSQL+Godaddy
- 关于权限系统的一些思考
- scrapy使用笔记
- 设置打包方式为war
- 二、Python第二课——变量命名规则及字符串变量相关函数
- filter过滤器_JavaWeb之 Filter(过滤器)
- 放弃耳机孔、放弃按键的手机我们是怎么接受并习惯的?
- nc加载不了java,用友NC系统使用过程中常见问题和解决方法
- 豆瓣9.6分,Scala编程圣经第5版隆重来袭
- 信息安全管理的效益分析
- 机翻福音-多种语言平行语料库资源
- 19湖大考研经验总结
- 银行软件测试怎么做的
- 专访|特赞CTO黄勇:微服务在互联网系统中如何实施?
- 利用Java+DOS批处理实现网站刷流量
- 下次遇到这种游戏,这种抽奖你还点吗?
热门文章
- Unity警告 Trying to Invoke method: PlayManager.ReturnTheMainMenu couldn‘t be called.
- 无线功率传输(WPT)及相关标准(包括Qi)
- cf 678E - Another Sith Tournament 【状压dp】
- 一分钟看懂Python中的 // 和 / 和 % 的用法区别
- 和菜头-当你在网络上被黑时
- nefuoj1487时空乱流
- android开发自定义相机镜像问题
- 复旦计算机学硕上岸,复旦大学工程与应用技术研究院电子信息2020年考研上岸前辈经验指导...
- ubuntu wps缺少字体_一个字体:系统风汜霰更纱黑
- dasda(大s打陈建州)