C#LINQ介绍

来源:互联网 发布:windows 运行spark 编辑:程序博客网 时间:2024/06/16 06:23

前言

最近在自学C#的过程中,突然发现LINQ(Language Integrated Query)这个名词,自己看了看MSDN,就决定摘录其中个人认为比较用知识点,供大家学习参考。

  • 首先看看微软MSDN的解释:
A query is an expression that retrieves data from a data source. Queries are usually expressed in a specialized query language. Different languages have been developed over time for the various types of data sources, for example SQL for relational databases and XQuery for XML.   Therefore, developers have had to learn a new query   language for each type of data source or data format that   they must support. LINQ simplifies this situation by offering a consistent model for working with data across various kinds of data sources and formats. In a LINQ query, you are always working with objects. You use the same basic coding patterns to query and transform data in XML documents, SQL databases, ADO.NET Datasets, .NET collections, and any other format for which a LINQ provider is available.

一大段英文,想必大家都不愿意去看这些,但是我觉得这段英文是十分有用的,下来自己翻译一下

一个查询是一个表达式,从数据源检索数据。查询通常表示在一个专门的查询语言。不同的语言已经发展了很长时间的各种类型的数据源,比如关系数据库的SQL和XQuery XML。因此,开发人员不得不学习一种新的查询语言对每个类型的数据源或数据格式,他们必须支持。LINQ简化这种情况提供了一个一致的模型在各种数据源和处理数据格式。在LINQ查询,你总是使用对象。你使用相同的基本编码模式在XML文档的查询和转换数据,SQL数据库、ADO。网络数据集。净集合,和任何其他格式的LINQ提供程序是可用的。
  • 查询三步走
All LINQ query operations consist of three distinct actions:Obtain the data source.Create the query.Execute the query.

举例说明LINQ的使用

class Program{        static void Main(string[] args)        {            // Specify the data source.            int[] scores = new int[] { 97, 92, 81, 60 };            // Define the query expression.            IEnumerable<int> scoreQuery =                from score  in scores                 where score > 80                 select score;            // Execute the query.            foreach (int i in scoreQuery)            {                Console.Write(i + " ");            }                    }   }
  • 代码之行结果如下:
    这里写图片描述

  • 下面的插图显示了完整的查询操作。在LINQ查询的执行不同的查询本身,换句话说你没有任何数据只是通过创建一个查询来检索变量。

这里写图片描述

以上为自己结合MSDN学习的点心得

1 0
原创粉丝点击