LINQ查询表达式基础
LINQ,语言集成查询(Language Integrated Query)是一组用C#和Visual Basic语言的扩展。
对于编写查询的开发人员来说,LINQ 最明显的“语言集成”部分是查询表达式。查询表达式是使用 C# 3.0 中引入的声明性查询语法编写的。通过使用查询语法,你甚至可以使用最少的代码对数据源执行复杂的筛选、排序和分组操作。你使用相同的基本查询表达式模式来查询和转换 SQL 数据库、ADO.NET 数据集、XML 文档和流以及 .NET 集合中的数据。
下面我们通过一些实际中常用的功能举例来学习一下。
1.简单数组的一些基本操作
查询数组中大于80的分数。
int[] scores = new int[] { , , , };
IEnumerable<int> scoreQuery = from score in scores
where score >
select score;
6
foreach(int score in scoreQuery)
{
Console.WriteLine(score);
}
查询数组中大于80的分数,按照升序ascending排列(默认为降序descending ),并且格式化结果。
int[] scores = new int[] { , , , };
IEnumerable<string> scoreQuery = from score in scores
where score >
orderby score ascending
select String.Format("Score: {0}",score);
foreach(string score in scoreQuery)
{
Console.WriteLine(score);
}
2.数组列表的一些基本操作
这里首先创建一个程序员Programer类,包含了ID,Name,Age,Language属性。
public class Programer
{
public int ID;
public string Name;
public int Age;
public string Language;
}
where
然后创建一个Programer类型的测试数组列表。查找编程语言使用C#的程序员。结果将输出Name1和Name4。
Programer[] programers = new Programer[]{
new Programer{ID=,Name="Name1",Age=,Language="C#"},
new Programer{ID=,Name="Name2",Age=,Language="Visual Basic"},
new Programer{ID=,Name="Name3",Age=,Language="C++"},
new Programer{ID=,Name="Name4",Age=,Language="C#"},
new Programer{ID=,Name="Name5",Age=,Language="C"}
};
IEnumerable<Programer> programerQuery =
from p in programers
where p.Language == "C#"
select p;
foreach(var item in programerQuery)
{
Console.WriteLine(item.Name);
}
where / orderby / select格式化数据
同样,这里也可以根据需要进行排序,对结果格式化等操作。下面查找编程语言使用C#的程序员,按Age升序排列,结果格式化输出Name和Age。
Programer[] programers = new Programer[]{
new Programer{ID=,Name="Name1",Age=,Language="C#"},
new Programer{ID=,Name="Name2",Age=,Language="Visual Basic"},
new Programer{ID=,Name="Name3",Age=,Language="C++"},
new Programer{ID=,Name="Name4",Age=,Language="C#"},
new Programer{ID=,Name="Name5",Age=,Language="C"}
};
IEnumerable<string> programerQuery =
from p in programers
where p.Language == "C#"
orderby p.Age ascending
select String.Format("Name: {0} , Age: {1}", p.Name, p.Age);
foreach(var item in programerQuery)
{
Console.WriteLine(item);
}
select部分数据
对于查询的结果,我们还可以根据需要选取部分数据。下面我们只需要程序员列表中的Name和Language数据。
Programer[] programers = new Programer[]{
new Programer{ID=,Name="Name1",Age=,Language="C#"},
new Programer{ID=,Name="Name2",Age=,Language="Visual Basic"},
new Programer{ID=,Name="Name3",Age=,Language="C++"},
new Programer{ID=,Name="Name4",Age=,Language="C#"},
new Programer{ID=,Name="Name5",Age=,Language="C"}
};
var newProgramers = from p in programers
select new
{
Name = p.Name,
Language = p.Language
};
foreach(var item in newProgramers)
{
Console.WriteLine("Name: {0} , Language: {1}", item.Name, item.Language);
}
group
当我们需要知道每种编程语言都有哪些程序员,我们就需要用到group分组功能。
Programer[] programers = new Programer[]{
new Programer{ID=,Name="Name1",Age=,Language="C#"},
new Programer{ID=,Name="Name2",Age=,Language="Visual Basic"},
new Programer{ID=,Name="Name3",Age=,Language="C++"},
new Programer{ID=,Name="Name4",Age=,Language="C#"},
new Programer{ID=,Name="Name5",Age=,Language="C"}
};
var programerGroupByLanguage = from p in programers
group p by p.Language;
foreach (var group in programerGroupByLanguage)
{
Console.WriteLine("Group: {0}", group.Key);
foreach(var item in group)
{
Console.WriteLine(item.Name);
}
Console.WriteLine();
}

group into
当我们想计算有多少程序员知道某种开发语言。在使用group 子句对开发语言进行分组后,可以使用Into子句将group 的子句结果存储在临时变量中。之后可以使用此变量来执行其他查询。
Programer[] programers = new Programer[]{
new Programer{ID=,Name="Name1",Age=,Language="C#"},
new Programer{ID=,Name="Name2",Age=,Language="Visual Basic"},
new Programer{ID=,Name="Name3",Age=,Language="C++"},
new Programer{ID=,Name="Name4",Age=,Language="C#"},
new Programer{ID=,Name="Name5",Age=,Language="C"}
};
var programerGroupByLanguage = from p in programers
group p by p.Language into languageGroup
select new
{
Language = languageGroup.Key,
Count = languageGroup.Count()
};
foreach (var group in programerGroupByLanguage)
{
Console.WriteLine("Language {0} have {1} programers", group.Language, group.Count);
}

3.多个数组列表的一些基本操作
创建两个类,Product产品类和Category类别类,每个产品分配一个类别。
public class Category
{
public int CategoryID { get; set; }
public string CategoryName { get; set; }
} public class Product
{
public int ProductID { get; set; }
public string ProductName { get; set; }
public int CategoryID { get; set; }
public string Description { get; set; }
}
接着创建一些Category类型和Product类型的测试数组列表。
from...from / from...join..on
下面我们按类别查询所有产品。
Category[] categories = new Category[]{
new Category{CategoryID=,CategoryName="TOYOTA"},
new Category{CategoryID=,CategoryName="BMW"},
new Category{CategoryID=,CategoryName="BENZ"},
new Category{CategoryID=,CategoryName="HONDA"},
new Category{CategoryID=,CategoryName="LEXUS"}
};
Product[] products = new Product[]{
new Product{ProductID=,ProductName="Car1",CategoryID=,Description="TOYOTA Car"},
new Product{ProductID=,ProductName="Car2",CategoryID=,Description="TOYOTA SUV"},
new Product{ProductID=,ProductName="Car3",CategoryID=,Description="BMW Car"},
new Product{ProductID=,ProductName="Car4",CategoryID=,Description="BMW SUV"},
new Product{ProductID=,ProductName="Car5",CategoryID=,Description="BENZ Car"},
new Product{ProductID=,ProductName="Car6",CategoryID=,Description="HONDA Car"},
new Product{ProductID=,ProductName="Car7",CategoryID=,Description="LEXUS Car"},
new Product{ProductID=,ProductName="Car8",CategoryID=,Description="LEXUS SUV"}
};
var productByCategoryResult1 = from c in categories
join p in products
on c.CategoryID equals p.CategoryID
select new
{
c.CategoryName,
p.ProductName,
p.Description
};
var productByCategoryResult2 = from c in categories
from p in products
where c.CategoryID ==p.CategoryID
select new
{
c.CategoryName,
p.ProductName,
p.Description
};
Console.WriteLine("Result 1:");
foreach(var item in productByCategoryResult1)
{
Console.WriteLine(item);
}
Console.WriteLine("Result 2:");
foreach (var item in productByCategoryResult2)
{
Console.WriteLine(item);
}

我们看到使用多个from和使用join查询出的结果是一样的。
分组into
当我们需要统计每种类别下的产品时,也就是对类别进行分组,只需加上into到某个变量语句即可,此时这个变量将自动对产品的结果集进行分组。
var productByCategoryResult = from c in categories
join p in products
on c.CategoryID equals p.CategoryID
into CategoryProducts
select new
{
c.CategoryName,
Products=CategoryProducts
}; foreach (var item in productByCategoryResult)
{
Console.WriteLine("Category: {0}",item.CategoryName);
foreach(var product in item.Products)
{
Console.WriteLine("{0} ({1})", product.ProductName, product.Description);
}
}

let
如果要将子表达式的结果存储在变量中以便来执行其他操作,可以使用Let子句。例如这里需要统计分配给类别的产品数量。
var productByCategoryResult = from c in categories
join p in products
on c.CategoryID equals p.CategoryID
into CategoryProducts
let productCount=CategoryProducts.Count()
select new
{
c.CategoryName,
Count=productCount
}; foreach (var item in productByCategoryResult)
{
Console.WriteLine("Category {0} have {1} products", item.CategoryName, item.Count);
}

好了,本篇就先到此,希望对你有所帮助,谢谢!
LINQ查询表达式基础的更多相关文章
- LINQ查询表达式(1) - 查询表达式基础
LINQ包括五个部分:LINQto Objects.LINQ to DataSets.LINQ to SQL.LINQ to Entities.LINQ to XML. 什么是查询?它有什么用途? “ ...
- Linq专题之创建Linq查询表达式
本节我们主要介绍一下如何创建查询集合类型,关系数据库类型,DataSet对象类型和XML类型的数据源的Linq查询表达式. 下面在实例代码ReadyCollectionData()函数创建了准备的数据 ...
- 2.3 LINQ查询表达式中 使用select子句 指定目标数据
本篇讲解LINQ查询的三种形式: 查询对象 自定义查询对象某个属性 查询匿名类型结果 [1.查询结果返回集合元素] 在LINQ查询中,select子句和from子句都是必备子句.LINQ查询表达式必须 ...
- LINQ查询表达式---------let子句
LINQ查询表达式---------let子句 let子句创建一个范围变量来存储结果,变量被创建后,不能修改或把其他表达式的结果重新赋值给它.此范围变量可以再后续的LINQ子句中使用. class P ...
- LINQ查询表达式---------join子句
LINQ查询表达式---------join子句 join 子句接受两个源序列作为输入. 每个序列中的元素都必须是可以与另一个序列中的相应属性进行比较的属性,或者包含一个这样的属性. join子句使用 ...
- LINQ查询表达式---------orderby子句
LINQ查询表达式---------orderby子句 LINQ可以按元素的一个或多个属性对元素进行排序. class Program { public class PerInfo { public ...
- LINQ查询表达式---------into
LINQ查询表达式---------into into 上下文关键字创建一个临时标识符,以便将 group.join 或 select 子句的结果存储到新的标识符 class Program { pu ...
- LINQ查询表达式---------group子句
LINQ查询表达式---------group子句 LINQ表达式必须以from子句开头,以select或group子句结束.使用guoup子句来返回元素分组后的结果.group 子句返回一个 IGr ...
- LINQ查询表达式---------select子句
LINQ查询表达式---------select子句 1.1常见的select子句查询 class Program { public class PerInfo { public int Id { g ...
随机推荐
- windows下查找指定端口被哪个程序占用
在Windows环境下,用netstat命令查看某个端口号是否占用,为哪个进程所占用. eg.查看端口号为61078被哪个程序占用 1.查看端口号为61079被哪个PID所占用:Netstat –an ...
- 《SpringMVC数据绑定入门》笔记
基本类型 最好使用封装类型 简单多数据&多层级对象 简单多数据 单个对象,直接使用属性名=值即可 多层级对象 属性.属性=值即可 同属性多对象 WebDataBinder只在当前类中生效,不是 ...
- iOS 之UICollectionView 之原理介绍
0. 简介 参考:支持重排的iOS9 UICollectionView 参考:http://www.cnblogs.com/langtianya/p/3902801.html 参考:http://ww ...
- vuejs 子组件传递父组件的第二种方式
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- Iphone安装铃声
PP助手 应用列表中打开铃声多多文档. 5点击铃声下载,找到下载的铃声,按下图所示步骤导出至电脑. 6在PP助手界面内,找到"视频音乐"标签,然后进入视频音乐分类下的铃声分类,点击 ...
- iOS 设置控件圆角、文字、字体
以按钮为例: UIButton btn = [UIButton new]; btn.layer.masksToBounds = YES; btn.layer.cornerRadius = 10.0; ...
- php短数组写法
在php 5.4+的版本中,可以使用 [] 来表示 array() 例子: // On PHP 5.1 $data = array("foo", "bar" ...
- bzoj3991 [Sdoi2015]寻宝游戏 set动态维护虚树+树链求并
题目大意:支持多次操作,增加或删除一个关键点 动态维护虚树边权和*2 分析:可以用树链求并的方法,最后减去虚树的根到1距离 注意到树链求并是所有点到根距离-所有dfn序相邻两点的LCA到根距离 找df ...
- JavaScript对象属性的基础教程指南
JavaScript是使用“对象化编程”的,或者叫“面向对象编程”的.所谓“对象化编程”,意思是把JavaScript能涉及的范围划分成大大小小的对象,对象下面还继续划分对象直至非常详细为止,所有的编 ...
- TCP实现P2P通信
Internet的迅速发展以及IPv4 地址数量的限制使得网络地址翻译(NAT,Network Address Trans2lation)设备得到广泛应用.NAT设备允许处于同一NAT后的多台主机共享 ...