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()); Console.WriteLine("Count: {0}" , newList2.Count());
Console.WriteLine("Value: {0}" , newList2.ElementAt());
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(, );
Console.WriteLine("Total Count: {0} ", intCollection.Count()); for(int i = ; i < intCollection.Count(); i++)
Console.WriteLine("Value at index {0} : {1}", i, intCollection.ElementAt(i));

Repeat

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

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

LINQ 学习路程 -- 查询操作 ElementAt, ElementAtOrDefault的更多相关文章

  1. LINQ 学习路程 -- 查询操作 Expression Tree

    表达式树就像是树形的数据结构,表达式树中的每一个节点都是表达式, 表达式树可以表示一个数学公式如:x<y.x.<.y都是一个表达式,并构成树形的数据结构 表达式树使lambda表达式的结构 ...

  2. LINQ 学习路程 -- 查询操作 OrderBy & OrderByDescending

    Sorting Operator Description OrderBy 通过给定的字段进行升序 降序 排序 OrderByDescending 通过给定字段进行降序排序,仅在方法查询中使用 Then ...

  3. LINQ 学习路程 -- 查询操作 Deferred Execution of LINQ Query 延迟执行

    延迟执行是指一个表达式的值延迟获取,知道它的值真正用到. 当你用foreach循环时,表达式才真正的执行. 延迟执行有个最重要的好处:它总是给你最新的数据 实现延迟运行 你可以使用yield关键字实现 ...

  4. LINQ 学习路程 -- 查询操作 Join

    Join操作是将两个集合联合 Joining Operators Usage Join 将两个序列连接并返回结果集 GroupJoin 根据key将两个序列连接返回,像是SQL中的Left Join ...

  5. LINQ 学习路程 -- 查询操作 where

    1.where Filtering Operators Description Where Returns values from the collection based on a predicat ...

  6. LINQ 学习路程 -- 查询操作 GroupBy ToLookUp

    Grouping Operators Description GroupBy GroupBy操作返回根据一些键值进行分组,每组代表IGrouping<TKey,TElement>对象 To ...

  7. LINQ 学习路程 -- 查询操作 let into关键字

    IList<Student> studentList = new List<Student>() { , StudentName = } , , StudentName = } ...

  8. LINQ 学习路程 -- 查询操作 Aggregate

    聚合操作执行数学的运算,如平均数.合计.总数.最大值.最小值 Method Description Aggregate 在集合上执行自定义聚集操作 Average 求平均数 Count 求集合的总数 ...

  9. LINQ 学习路程 -- 查询操作 Select, SelectMany

    IList<Student> studentList = new List<Student>() { , StudentName = "John" }, , ...

随机推荐

  1. C----------输入一组整数,求出这组数字子序列和中的最大值,只要求出最大子序列的和,不必求出最大值对应的序列。

    © 版权声明:本文为博主原创文章,转载请注明出处 代码: #include <stdio.h> #include <stdlib.h> #define GET_ARRAY_LE ...

  2. Android实现一键获取课程成绩dome

    欢迎转载但请标明出处:http://blog.csdn.net/android_for_james/article/details/50984493 两周废寝忘食的创作最终成功了,如今拿出来分享一下. ...

  3. PyQt5 Function Parameter Declaration

    addWidget self.lcd = QLCDNumber() grid.addWidget(self.lcd,0,0,3,0) grid.setSpacing(10) void QGridLay ...

  4. lua学习笔记(五)

    语句     赋值         多重赋值         a, b, c, d = 1, 2, 3, 4         a, b, c = 1, 2         assert(c == ni ...

  5. inside when() you don't call method on mock but on some other object

    错误原因:调用静态方法,要事先引入静态类,否则mock的时候会默认为测试的类 解决方法:@PrepareForTest({SecurityContextHolder.class})引入静态类 注:@P ...

  6. webview长按保存图片

    private String imgurl = ""; /***     * 功能:长按图片保存到手机     */    @Override    public void onC ...

  7. Unix编程第7章 进程环境

    准备雄心勃勃的看完APUE,但是总感觉看着看着就像进入一本字典,很多地方都是介绍函数的用法的,但是给出例子远不及函数介绍的多.而且这本书还是个大部头呢.第7章的讲的进程环境,进程是程序设计中一个比较重 ...

  8. use-svn-cmd(Linux)

    SVN是Subversion的简称,是一个开放源代码的版本控制系统,它采用了分支管理系统,是一个跨平台的软件,支持大多数常见的操作系统. svn命令行使用: 1.查看svn所支持的全部命令 $ svn ...

  9. Django下实现HelloWorld

    我的实现工具:window10 在window10 下面,实现第一个Django的HelloWorld项目. 1.创建一个项目 确保你的电脑上装了python和Django.我的是在python2.7 ...

  10. 时下世界上最先进的液晶面板技术---ips

    IPS是英文In-Plane Switching的缩写,英文含义为平面转换屏幕技术,由日立公司于2001推出的液晶面板技术,俗称“Super TFT”,是目前世界上最先进的液晶面板技术,目前已经广泛使 ...