zoukankan      html  css  js  c++  java
  • 3. Linq操作符

    条件操作符 where

    映射操作符 select

    LINQ query syntax must end with a Select or GroupBy clause

    public class Student{
        public int StudentID { get; set; }
        public string StudentName { get; set; }
        public int Age { get; set; }
    }
    
    
    IList<Student> studentList = new List<Student>() {
        new Student() { StudentID = 1, StudentName = "John" },
        new Student() { StudentID = 2, StudentName = "Moin" },
        new Student() { StudentID = 3, StudentName = "Bill" },
        new Student() { StudentID = 4, StudentName = "Ram" },
        new Student() { StudentID = 5, StudentName = "Ron" }
    };
    
    //只是查询姓名
    var selectResult = from s in studentList
                       select s.StudentName;
    
    IList<Student> studentList = new List<Student>() {
        new Student() { StudentID = 1, StudentName = "John", Age = 13 } ,
        new Student() { StudentID = 2, StudentName = "Moin",  Age = 21 } ,
        new Student() { StudentID = 3, StudentName = "Bill",  Age = 18 } ,
        new Student() { StudentID = 4, StudentName = "Ram" , Age = 20 } ,
        new Student() { StudentID = 5, StudentName = "Ron" , Age = 15 }
    };
    
    //这里面可以针对不同条件对数据做不同处理
    // returns collection of anonymous objects with Name and Age property
    var selectResult = from s in studentList
                       select new { Name = "Mr. " + s.StudentName, Age =s.Age==15?1:s.Age };
    
    // iterate selectResult
    foreach (var item in selectResult)
        Console.WriteLine("Student Name: {0}, Age: {1}", item.Name, item.Age);

    分割操作符 Partition operators

    Take, Skip, TakeWhile and SkipWhile methods to partition the input sequence .You can get a slice of the input sequence as the output sequence.

    排序操作符 Ordering operators

    The orderby keyword, along with descending, and the OrderBy, ThenBy, OrderbyDescending and ThenByDescending LINQ queries are used to sort data output.

    分组操作符 Grouping operators

    The GroupBy and into operators organize a sequence into buckets.

    集合操作符 Set Operators

    这些操作符提供对多个集合的数据对比, 提供不同集合直接的交集,并集,不重复和差异的

    转换操作符 Conversion operators

    转化成Array List Dictionary 或指定的元素类型

    元素操作符 Element operators

    The methods First, FirstOrDefault, Last, LastOrDefault, and ElementAt retrieve elements based on the position of that element in the sequence.

    • FirstOrDefault 返回第一个元素,当没有元素的时候,返回对应类型的默认值;

    生成操作符 Generate sequences

    量化操作符 Quantifying members

    all any

    聚合操作符 Aggregator operators

    count

    序列操作符 Sequence operations

    懒查询 Eager and lazy query execution

    Join 操作符 Join operations

  • 相关阅读:
    [BZOJ1578] [Usaco2009 Feb]Stock Market 股票市场(DP)
    [BZOJ1576] [Usaco2009 Jan]安全路经Travel(堆优化dijk + (并查集 || 树剖))
    [BZOJ1575] [Usaco2009 Jan]气象牛Baric(DP)
    [BZOJ1574] [Usaco2009 Jan]地震损坏Damage(贪心 + dfs)
    [BZOJ1572] [Usaco2009 Open]工作安排Job(贪心 + 堆)
    [luoguP2862] [USACO06JAN]把牛Corral the Cows(二分 + 乱搞)
    [luoguP3668] [USACO17OPEN]Modern Art 2 现代艺术2(栈)
    [luoguP3608] [USACO17JAN]Balanced Photo平衡的照片(树状数组 + 离散化)
    [luoguP3110] [USACO14DEC]驮运Piggy Back(SPFA || BFS)
    [BZOJ2342] [Shoi2011]双倍回文(manacher)
  • 原文地址:https://www.cnblogs.com/maanshancss/p/13086830.html
Copyright © 2011-2022 走看看