• LINQ 学习路程 -- 查询操作 ElementAt, ElementAtOrDefault


    Element Operators (Methods)Description
    ElementAt 返回指定索引的元素,如果索引超过集合长度,则抛出异常
    ElementAtOrDefault 返回指定索引的元素,如果索引超过集合长度,则返回元素的默认值,不抛出异常
    First 返回集合的第一个元素,可以根据指定条件返回
    FirstOrDefault 返回集合的第一个元素,可以根据指定条件返回,如果集合不存在元素,则返回元素的默认值
    Last 返回集合中的最后一个元素,可以根据条件返回
    LastOrDefault 返回集合中的最后一个元素,可以根据条件返回,如果集合不存在,则返回元素的默认值
    Single 返回集合中的一个元素,可以根据条件返回,如果集合不在存元素或有多个元素,则抛出异常
    SingleOrDefault 返回集合中的一个元素,可以根据条件返回,如果集合不在存元素,则返回元素默认值,如果存在多个元素,则抛出异常
    public static TSource First<TSource>(this IEnumerable<TSource> source);
    
    public static TSource First<TSource>(this IEnumerable<TSource> source, Func<TSource, bool> predicate);
    
    public static TSource FirstOrDefault<TSource>(this IEnumerable<TSource> source);
    
    public static TSource FirstOrDefault<TSource>(this IEnumerable<TSource> source, Func<TSource, bool> predicate);
    public static TSource Last<TSource>(this IEnumerable<TSource> source);
    
    public static TSource Last<TSource>(this IEnumerable<TSource> source, Func<TSource, bool> predicate);
    
    public static TSource LastOrDefault<TSource>(this IEnumerable<TSource> source);
    
    public static TSource LastOrDefault<TSource>(this IEnumerable<TSource> source, Func<TSource, bool> predicate);
    public static TSource Single<TSource>(this IEnumerable<TSource> source);
    
    public static TSource Single<TSource>(this IEnumerable<TSource> source, Func<TSource, bool> predicate);
    
    public static TSource SingleOrDefault<TSource>(this IEnumerable<TSource> source);
    
    public static TSource SingleOrDefault<TSource>(this IEnumerable<TSource> source, Func<TSource, bool> predicate);

    SequenceEqual

    判断集合相等‘

    如果集合的元素是简单类型,则判断两个集合的元素个数,元素值,出现的位置是否一样

    如果集合的元素是复杂类型,则判断两个集合的元素引用是否相同、元素个数,元素值,出现的位置是否一样

    如果要判断集合元素为复杂类型的值是否相等,则要实现IQualityComparer<T>接口

    class StudentComparer : IEqualityComparer<Student>
    {
        public bool Equals(Student x, Student y)
        {
            if (x.StudentID == y.StudentID && x.StudentName.ToLower() == y.StudentName.ToLower())
                return true;
    
            return false;
        }
    
        public int GetHashCode(Student obj)
        {
            return obj.GetHashCode();
        }
    }

    Concat

    连接两个元素类型相同的集合,并返回新的集合

    IList<string> collection1 = new List<string>() { "One", "Two", "Three" };
    IList<string> collection2 = new List<string>() { "Five", "Six"};
    
    var collection3 = collection1.Concat(collection2);
    
    foreach (string str in collection3)
        Console.WriteLine(str);

    DefaultIfEmpty

    如果集合元素个数为0,调用DefaultIfEmpty后,则返回一个新的集合,且包含一个元素(元素值为默认值)

     另一个方法重载接收一个参数,代替默认值返回

    IList<string> emptyList = new List<string>();
    
    var newList1 = emptyList.DefaultIfEmpty(); 
    var newList2 = emptyList.DefaultIfEmpty("None"); 
    
    Console.WriteLine("Count: {0}" , newList1.Count());
    Console.WriteLine("Value: {0}" , newList1.ElementAt(0));
    
    Console.WriteLine("Count: {0}" , newList2.Count());
    Console.WriteLine("Value: {0}" , newList2.ElementAt(0));
    MethodDescription
    Empty 返回空集合
    Range Generates collection of IEnumerable<T> type with specified number of elements with sequential values, starting from first element.
    Repeat Generates a collection of IEnumerable<T> type with specified number of elements and each element contains same specified value.

     Empty不是IEnumerable的扩展方法,而是Enumerable的静态方法

    var emptyCollection1 = Enumerable.Empty<string>();
    var emptyCollection2 = Enumerable.Empty<Student>();
    
    Console.WriteLine("Count: {0} ", emptyCollection1.Count());
    Console.WriteLine("Type: {0} ", emptyCollection1.GetType().Name );
    
    Console.WriteLine("Count: {0} ",emptyCollection2.Count());
    Console.WriteLine("Type: {0} ", emptyCollection2.GetType().Name );

    Range 

    方法返回指定元素个数的集合,集合以给定的值开始 (元素类型为int)

    var intCollection = Enumerable.Range(10, 10);
    Console.WriteLine("Total Count: {0} ", intCollection.Count());
    
    for(int i = 0; i < intCollection.Count(); i++)
        Console.WriteLine("Value at index {0} : {1}", i, intCollection.ElementAt(i));

    Repeat

     返回指定元素个数的集合,且元素的值一样

    var intCollection = Enumerable.Repeat<int>(10, 10);
    Console.WriteLine("Total Count: {0} ", intCollection.Count());
    
    for(int i = 0; i < intCollection.Count(); i++)
        Console.WriteLine("Value at index {0} : {1}", i, intCollection.ElementAt(i));
  • 相关阅读:
    BZOJ 3529 [Sdoi2014]数表
    bzoj 3195 [Jxoi2012]奇怪的道路
    BZOJ 4720 [Noip2016]换教室
    BZOJ 2160 拉拉队排练
    BZOJ 1031 [JSOI2007]字符加密Cipher 后缀数组教程
    BZOJ 1002 [FJOI2007]轮状病毒
    欧拉定理、拓展欧拉定理及其应用(欧拉降幂法)
    算术基本定理解析及其应用
    The 15th Zhejiang Provincial Collegiate Programming Contest(部分题解)
    网络流解析及其应用
  • 原文地址:https://www.cnblogs.com/lanpingwang/p/6607355.html
Copyright © 2020-2023  润新知