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));

 

posted @ 2017-03-23 21:10  蓝平凡  阅读(2646)  评论(0编辑  收藏  举报