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

  • 相关阅读:
    python,jsonpath提取json数据
    [.Net] Web API 本地化与全球化
    缺省源
    组合恒等式
    20210925衡阳八中多校联测
    codeforces赛后总结——1556D. Take a Guess
    codeforces赛后总结——1556C. Compressed Bracket Sequence
    阿里云(Ubuntu20.04)搭建wordpress全流程——附图超详细版
    Linux常用命令行操作
    阿里云服务器增加监听端口
  • 原文地址:https://www.cnblogs.com/qfb620/p/1113321.html
Copyright © 2011-2022 走看看