zoukankan      html  css  js  c++  java
  • C#学习笔记之——LINQ

    LINQ(Language Integrated Query)

    LINQ是.net框架的扩展,它允许我们以使用SQL查询数据库的方式来查询数据集合。

    使用LINQ,你可以从数据库,程序对象的集合以及XML文档中查询数据。

    查询语句

     1 using System;
     2 using System.Linq;
     3 
     4 namespace Test
     5 {
     6     class Program
     7     {
     8         static void Main(string[] args)
     9         {
    10             int[] numbers = { 2, 5, 28, 31, 17, 17, 42 };
    11 
    12             var numsQuery = from n in numbers
    13                             where n < 20
    14                             select n;//查询语句
    15 
    16             var numsMethod = numbers.Where(x => x < 20);//方法语句
    17 
    18             int numsCount = (from n in numbers
    19                              where n < 20
    20                              select n).Count();//两种结合
    21 
    22             foreach (var x in numsQuery)
    23             {
    24                 Console.Write("{0} ", x);
    25             }
    26             Console.WriteLine();
    27 
    28             foreach (var x in numsMethod)
    29             {
    30                 Console.Write("{0} ", x);
    31             }
    32             Console.WriteLine();
    33 
    34             Console.WriteLine(numsCount);
    35         }
    36     }
    37 }

    from 迭代变量 in 要查询的集合名字

    join 指定另外的集合和ID引用它 on 第一个集合的项 equals 第二个集合的项

    from...let...where:

    static void Main()
    {
        var groupA = new[] {3, 4, 5, 6};
        var groupB = new[] {6, 7, 8, 9};
        
        var someInts = from a in groupA
                        from b in groupB
                        let sum = a + b
                        where sum == 12
                        select new {a, b, sum};
        foreach (var a in someInts)
            Console.WriteLine(a);
    }

    from...orderby...select

    static void Main()
    {
        var group = new[] {8, 6, 3, 9, 2};
        
        var result = from 
    }

    待补充。。

  • 相关阅读:
    node
    github
    [模块] pdf转图片-pdf2image
    python 15 自定义模块 随机数 时间模块
    python 14 装饰器
    python 13 内置函数II 匿名函数 闭包
    python 12 生成器 列表推导式 内置函数I
    python 11 函数名 迭代器
    python 10 形参角度 名称空间 加载顺序
    python 09 函数参数初识
  • 原文地址:https://www.cnblogs.com/AlinaL/p/12852120.html
Copyright © 2011-2022 走看看