zoukankan      html  css  js  c++  java
  • Linq学习2

    参考书籍 《C# in a nutshell 8.0》

    基本数据单元是序列和元素,序列是任何实现了IEnumberable<T>接口的对象,而其中的每一项叫做一个元素。

    Names就是一个序列,"Tom","Dick","Harry"就是元素。

    Names 表示内存中的本地对象的集合,称之为"本地序列"

    We call this a local sequence because it represents a local collection
    of objects in memory.

    查询运算符(query operator)是转换序列的方法。一个典型的查询运算符,接受一个输入序列,转换产生一个输出序列。

    A query operator is a method that transforms a sequence. A typical
    query operator accepts an input sequence and emits a transformed
    output sequence.

    在命名空间System.Linq中的Enumerable类,定义了约40种查询操作(query operators--所有都是以静态扩展方法的形式来实现的。

    称为标准查询运算符。

    In the Enumerable class in System.Linq,there are around 40 query operators—all implemented as static extension methods. These are called standard query operators.


    NOTE
    Queries that operate over local sequences are called local queries or LINQ-to-objects queries.
    LINQ also supports sequences that can be dynamically fed from a remote data source
    such as a SQL Server database. These sequences additionally implement the IQueryable<T> interface and are supported through a matching set of standard query operators in the Queryable class. We discuss this further in "Interpreted Queries"

     

     

     

    ///////////////////////////////////////////////////

     

    A query is an expression that, when enumerated, transforms sequences with query operators. The simplest query comprises one
    input sequence and one operator. For instance, we can apply the
    Where operator on a simple array to extract those strings whose
    length is at least four characters, as follows:

     

     

    using System;

    using System.Collections.Generic;

    using System.Linq;

     

    namespace csharpnut_linq001

    {

    class Program

    {

    static void Main(string[] args)

    {

    string[] names = { "孙悟空", "猪八戒", "唐僧", "沙和尚", "白龙马" };

    IEnumerable<string> filteredNames = Enumerable.Where(names, n => n.Length >= 3);

    foreach (string name in filteredNames)

    {

    Console.WriteLine(name);

    }

     

    }

    }

    }

    输出结果:

     

    改进一下:

    书中说var不好,我倒没觉得

    Lambda表达式

    n => n.Length >= 3

    可以把=>看做是函数表达。

     

    namespace csharpnut_linq001

    {

    class Program

    {

    static void Main(string[] args)

    {

    string[] names = { "孙悟空", "猪八戒", "唐僧", "沙和尚", "白龙马" };

    //IEnumerable<string> filteredNames = Enumerable.Where(names, n => n.Contains(""));

                //IEnumerable<string> filteredNames = names.Where(n => n.Contains(""));

                var filteredNames = names.Where(n => n.Contains(""));

    foreach (string name in filteredNames)

    {

    Console.WriteLine(name);

    }

     

    }

    }

    }

    到目前为止,我们建立queries(查询),使用的是 extension method 扩展方法 lambda expression lambda表达式。

    Fluent syntax "流利语法" 就是上面两种组合

    Query expression syntax 查询表达式语法(这种是微软推荐的用法)

    So far, we've built queries using extension methods and lambda expressions. As you'll see shortly, this strategy is highly composable in that it allows the chaining of query operators. In this book, we refer to this as fluent syntax. C# also provides another syntax for writing queries, called query expression syntax. Here's our preceding query written as a query expression:

    查询表达式语法,其中select放在后面,和SQL语句稍微不一样点。
    IEnumerable<string> filteredNames = from n in names

                                        where n.Contains ("a")

                                        select n;


    Fluent syntax and query syntax are complementary. In the following
    two sections, we explore each in more detail.

    流式语法fluent syntax,这不是JavaScript的链式语法吗?

     

    可以分开写:

  • 相关阅读:
    使用sql语句查询表结构
    plsql出现录相机后卡屏解决方法
    oracle的“ORA-01480:STR绑定值的结尾Null字符缺失”错误
    oracle创建表空间并对用户赋权
    Scrapy安装错误(error: Microsoft Visual C++ 14.0 is required. Get it with "Microsoft Visual C++ Build Tools": http://landinghub.visualstudio.com/visual-cpp-build-tools)
    震惊你不知道的python
    django.core.exceptions.ImproperlyConfigured: Error loading MySQLdb module: No module named 'MySQLdb'
    python3 ImportError: No module named 'ConfigParser'
    python import报错
    No migrations to apply(django不能创建数据库中的表的问题)
  • 原文地址:https://www.cnblogs.com/ifconfig/p/13189833.html
Copyright © 2011-2022 走看看