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 ...
随机推荐
- photoshop如何快速切图
作为业余爱好,之前都是用比较笨的方法切图,甚至用裁剪工具一张一张的切. 后来知道用切片工具,但也仅限于互不重叠的图片. 在工作中实际使用时才发现实在是太慢了,慢到上级自己说你不用做了,我来吧. 其实, ...
- STM32 对内部FLASH读写接口函数(转)
源:STM32 对内部FLASH读写接口函数 因为要用内部FLASH代替外部EEPROM,把参数放在STM32的0x08000000+320K处,其中20K是bootloader,300K是应用程序. ...
- JUCE_FIFO实现分析
JUCE_FIFO代码简单剖析,此处的剖析就是给自己留一个方便查询的内容:不做太多内容的要求,只做简单的记录:本文以初学者的角度来些,大牛请飘过O(∩_∩)O FIFO的功能在博客的其他帖子里面已经有 ...
- iOS UIActivityIndicatorView 的使用
UIActivityIndicatorView 非常简单 ,就是一个转圈圈的控件:http://blog.csdn.net/zhaopenghhhhhh/article/details/1209265 ...
- selenium IE 浏览器驱动下载地方
download: http://selenium-release.storage.googleapis.com/index.html
- Java编译时出现No enclosing instance of type XXX is accessible.
今天在编译Java程序的时候出现以下错误: No enclosing instance of type Main is accessible. Must qualify the allocation ...
- Centos7.2 编译安装PHP7
PHP7,编译安装: 环境:centos7.2 (注意:因为我用的nginx, 此配置参数没有考虑到apache,所以不合适需要用apache的朋友照搬过去运行,但是可以参考.) 直接下载P ...
- .Net普通三层 到 工厂模式->线程内唯一+单元工作模式->WebService分布式三层
在软件世界分层的思想无处不在 主要是为了提高软件系统的维护性,扩展性,复用性和解耦等 软件的三层构架是一种最基本的分层思想的体现 结构图大体如下: 如此一来,开发人员可以只关注其中一层,而无需关心下一 ...
- C#常用网址
C# 编程指南 https://msdn.microsoft.com/zh-cn/library/67ef8sbd.aspx
- @pathvariable 注解
1.4. @PathVariable 注解 带占位符的 URL 是 Spring3.0 新增的功能,该功能在SpringMVC 向 REST 目标挺进发展过程中具有里程碑的意义 通过 @PathVar ...