LINQ查询字句概述
1.查询(Query)是一组指令,这些指令可以从一个或多个给定的数据源中检索数据,并指定检索结果的数据类型和表现形式。
2.查询表达式是一种用查询语法表示的表达式,由一组用类似于SQL的生明性语法编写的·字句组成。
3.每个子句可以包含一个或多个C#表达式,而这些表达式本身又可能是查询表达式或包含查询表达式。
4.查询表达式和其他表达式一样,可以用在C#表达式有效的任何上下文中。
常用字句
LINQ查询表达式包含8句常用字句像子句:from丶where丶select字句等
子句 说明
from..in子句 | 指定查询操作的数据源和变量范围 |
where子句 | 筛选元素的逻辑条件,一般由逻辑运算符(逻辑“与 &&”丶逻辑“或 ||”组成) |
select子句 | 指定查询结果的类型和表现形式 |
order...by子句 | 对查询结果进行排序,可以为“升序”或“降序” |
group...by子句 | 对查询结果进行分组 |
into子句 | 提供一个临时标示符,充当join丶group丶select子句的结果 |
join子句 | 连接多个查询操作的数据源 |
let子句 | 引入用于储存查询表达式的子表达式结果的范围变量 |
在第一个from子句和最后一个select或group子句之间,查询表达式可以包含一个或多个where丶order by丶group丶join丶let子句甚至from子句
另外,join和group子句还可以使用into子句指定临时标识符号
from...in 子句
LINQ查询表达式必须以from子句开头。如果该查询还包含子查询,那么子查询表达式也要以form开头。from子句指定查询操作的数据源和变量范围。其中,数据源不但包括查询本身的数据源还包括子查询数据源。在LINQ查询中第一步通常是指定数据源。
例
select * from Stuinfo where name in(select name from stu where ID=1)
select * from Stuinfo where name not in(select name from stu where ID=1)
in 等值连接,用来查找多表相同字段数据
not in 不等值连接,用来查找不存在的数据
where 子句
一个查询表达式可不包含where子句,也可包含一个或多个where子句。一个where子句可以包含一个或多个布尔条件类型表达式。实际上就是用户在筛选器中加入条件筛选排除哪些内容
这些运算符可在where子句中使用(">",">=","<","<=","=","<>"或"!=","!>","!<")
例
var queryLondonCustomers = from cust in customerswhere cust.City == "London"select cust;
select 子句
用于查询用户数据 当结果被储存在一个结果表中称为结果表
常见select语法为
select 列名 from 表名
select * from 表名
其中
*代表全部表
order...by 子句
可以指定多个键,以便执行一个或多个次要排序操作。排序是由针对元素类型的默认比较器执行的。默认排序顺序为升序。
group...by 子句
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 fruits
orderby fruit //"ascending" is default
select fruit;
// Query for descending sort.
IEnumerable<string> sortDescendingQuery =
from w in fruits
orderby w descending
select 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
cherry
Descending:
cherry
blueberry
apple
*/
group...by子句
通过一定规则将自个数据集划分为若干小区域,然后针对若干小区域进行处理
例:
- SELECT 列名1,dbo.aggregate_function(列名2)
- FROM table_name
- GROUP BY 列名1,列名2
into 子句
引用组操作的结果,可以使用 into 关键字来创建可进一步查询的标识符。 下面的查询只返回那些包含两个以上的客户的组
// custQuery is an IEnumerable<IGrouping<string, Customer>>
var custQuery =from cust in customers
<span style="color:#3366ff;">group</span> cust <span style="color:#3366ff;">by</span> cust.City <span style="color:#3366ff;">into </span>custGroup
where custGroup.Count() > 2
orderby custGroup.Key
select custGroup;
join 子句
使用 join 子句可以将来自不同源序列并且在对象模型中没有直接关系的元素相关联。 唯一的要求是每个源中的元素需要共享某个可以进行比较以判断是否相等的值。例如,食品经销商可能具有某种产品的供应商列表以及买主列表。例如,可以使用 join 子句创建该产品同一指定地区供应商和买主的列表
join可实现以下三种联接关系:
1.内部链接 要求连接的数据关系必须同时满足被联接的两个数据源。和SQL中的inner join子句相似
2.分组链接 包含into子句中的join子句
3.左外部链接 与SQL中的left join子句比较相似,它将返回第一个集合的每一个元素,而无论该元素在第二个集合中是否有相关元素
下面三类代码分别是:内部链接丶分组链接丶左外部链接
var innerJoinQuery =
from category in categories
<span style="color:#3366ff;">join</span> prod in products on category.ID equals prod.CategoryID
select new { ProductName = prod.Name, Category = category.Name }; //produces flat sequence
var innerJoinQuery =
from category in categories
<span style="color:#3366ff;">join</span> prod in products on category.ID equals prod.CategoryID
select new { ProductName = prod.Name, Category = category.Name }; //produces flat sequence
var leftOuterJoinQuery =
from category in categories
join prod in products on category.ID equals prod.CategoryID into prodGroup
from item in prodGroup.DefaultIfEmpty(new Product{Name = String.Empty, CategoryID = 0})
select new { CatName = category.Name, ProdName = item.Name };
let 子句
let子句指定的范围变量的值只能通过初始化操作进行赋值,范围变量的值一旦初始化,将不再被改变。
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 strings
let words = sentence.Split(' ')
from word in words
let 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
*/
如有不足,请批评指正,谢谢