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

 

 

 

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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM