zoukankan      html  css  js  c++  java
  • 学习LinQ

    LINQ是什么?
    它是Language Integrated Query。
    当我们要对数据库表进行查询的时候,我们一定会编写"select * from sometable where ID = .."的语句。好,那我们现在根据LINQ的语法,完全可以将我们熟悉的SQL中像"select","from","where"等语句在.NET Framework环境中顺利使用并且大大提高开发的效率。


    例如:

    public void Linq6() {
        int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 };

        var numsPlusOne =
            from n in numbers
            select n + 1;
        
        Console.WriteLine("Numbers + 1:");
        foreach (var i in numsPlusOne) {
            Console.WriteLine(i);
        }
    }

    Result

    Numbers + 1:
    6
    5
    2
    4
    10
    9
    7
    8
    3
    1


    再如:

    public void Linq10() {
        int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 };
        string[] strings = { "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine" };

        var digitOddEvens =
            from n in numbers
            select new {Digit = strings[n], Even = (n % 2 == 0)};

        foreach (var d in digitOddEvens) {
            Console.WriteLine("The digit {0} is {1}.", d.Digit, d.Even ? "even" : "odd");
        }
    }

    Result

    The digit five is odd.
    The digit four is even.
    The digit one is odd.
    The digit three is odd.
    The digit nine is odd.
    The digit eight is even.
    The digit six is even.
    The digit seven is odd.
    The digit two is even.
    The digit zero is even.


    还有:

    public void Linq12() {
        int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 };

        var numsInPlace = numbers.Select((num, index) => new {Num = num, InPlace = (num == index)});

        Console.WriteLine("Number: In-place?");
        foreach (var n in numsInPlace) {
            Console.WriteLine("{0}: {1}", n.Num, n.InPlace);
        }
    }

    Result

    Number: In-place?
    5: False
    4: False
    1: False
    3: True
    9: False
    8: False
    6: True
    7: True
    2: False
    0: False

  • 相关阅读:
    LOJ #6008. 「网络流 24 题」餐巾计划
    P2144 [FJOI2007]轮状病毒
    随记
    1010: [HNOI2008]玩具装箱toy(斜率优化)
    HDU 3507 Print Article(斜率优化)
    4819: [Sdoi2017]新生舞会(分数规划)
    POJ 2976 Dropping tests(01分数规划)
    spoj 104 Highways(Matrix-tree定理)
    dp专练
    4152: [AMPPZ2014]The Captain
  • 原文地址:https://www.cnblogs.com/qfb620/p/1113321.html
Copyright © 2011-2022 走看看