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

  • 相关阅读:
    21. 合并两个有序链表
    169. 多数元素
    关于快速幂取模
    IO帮助类
    XML序列化
    字符操作普通帮助类
    判断是否是手机
    C#命名规范汇总12条
    xamarin android如何将Java.Lang.Object类型转成C#类型
    mqtt服务器apollo的搭建和测试工具paho的使用
  • 原文地址:https://www.cnblogs.com/qfb620/p/1113321.html
Copyright © 2011-2022 走看看