LINQ 学习路程 -- 查询操作 ElementAt, ElementAtOrDefault
2024-09-06 19:30:55
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));
最新文章
- 如何在Zabbix上安装MySQL监控插件PMP
- [HTML/CSS] ul元素居中处理
- $anchorScroll和$cache
- Mysql慢查询操作梳理
- 使用Windows Live Writer发布日志
- CSS图片垂直居中方法
- 软键盘 输入法管理器 InputMethodManager
- C#Log4net日志记录组件的使用
- IDL 自己定义功能
- 如何提高maven的下载速度:享受一下mvn时飞的感觉
- javascript 计算两个日期的差值
- 用html+css+js做打地鼠小游戏
- [日常] NOIWC 2018爆零记
- Java开发笔记(三十四)字符串的赋值及类型转换
- TabLayout下划线指示器自适应文字宽度
- 转:MD5(Message-Digest Algorithm 一种哈希算法)
- C++程序设计方法4:函数模板
- ES6 let const 声明变量 块级作用域
- 中国移动CMPP协议、联通SGIP协议、电信SMGP协议短信网关
- 【问题收录】Ubuntu14.04连接两个双显示器失败的解决方案