C#LINQ介绍

前言

最近在自学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
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值