Sch*_*999 144 .net c# linq collections ienumerable
我爱string.IsNullOrEmpty
方法.我很想拥有能够为IEnumerable提供相同功能的东西.有这样的吗?也许一些收集助手班?我问的原因是,在if
语句中,如果模式是代码,代码看起来很混乱(mylist != null && mylist.Any())
.拥有它会更加清洁Foo.IsAny(myList)
.
这篇文章没有给出答案:IEnumerable是空的?.
Mar*_*ell 176
当然你可以这样写:
public static class Utils {
public static bool IsAny<T>(this IEnumerable<T> data) {
return data != null && data.Any();
}
}
Run Code Online (Sandbox Code Playgroud)
但是,要小心并非所有序列都是可重复的; 一般来说,我宁愿只走一次,以防万一.
Mat*_*eer 117
public static bool IsNullOrEmpty<T>(this IEnumerable<T> enumerable) {
return enumerable == null || !enumerable.Any();
}
Run Code Online (Sandbox Code Playgroud)
yoy*_*oyo 21
这是@Matt Greer的有用答案的修改版本,其中包含一个静态包装类,因此您可以将其复制粘贴到新的源文件中,不依赖于Linq,并添加泛型IEnumerable<T>
重载,以避免值类型的装箱非通用版本会发生这种情况.[编辑:请注意,使用IEnumerable<T>
不会阻止枚举器的装箱,鸭子打字无法阻止,但至少在值类型集合中的元素不会被装箱.]
using System.Collections;
using System.Collections.Generic;
public static class IsNullOrEmptyExtension
{
public static bool IsNullOrEmpty(this IEnumerable source)
{
if (source != null)
{
foreach (object obj in source)
{
return false;
}
}
return true;
}
public static bool IsNullOrEmpty<T>(this IEnumerable<T> source)
{
if (source != null)
{
foreach (T obj in source)
{
return false;
}
}
return true;
}
}
Run Code Online (Sandbox Code Playgroud)
Dar*_*ren 15
另一种方法是获取Enumerator并调用MoveNext()方法以查看是否有任何项:
if (mylist != null && mylist.GetEnumerator().MoveNext())
{
// The list is not null or empty
}
Run Code Online (Sandbox Code Playgroud)
这适用于IEnumerable以及IEnumerable <T>.
小智 6
我这样做,利用一些现代的C#功能:
选项1)
public static class Utils {
public static bool IsNullOrEmpty<T>(this IEnumerable<T> list) {
return !(list?.Any() ?? false);
}
}
Run Code Online (Sandbox Code Playgroud)
选项2)
public static class Utils {
public static bool IsNullOrEmpty<T>(this IEnumerable<T> list) {
return !(list?.Any()).GetValueOrDefault();
}
}
Run Code Online (Sandbox Code Playgroud)
顺便说一句,永远不要使用Count == 0
或Count() == 0
只是检查一个集合是否为空.总是使用Linq的.Any()
if (collection?.Any() == true){
// if collection contains more than one item
}
if (collection?.Any() != true){
// if collection is null
// if collection does not contain any item
}
Run Code Online (Sandbox Code Playgroud)
从C#6开始,您可以使用null传播:myList?.Any() == true
如果您仍然觉得这个问题过于冗长或不喜欢使用一种很好的扩展方法,我会推荐Matt Greer和Marc Gravell的答案,但其中有些扩展功能可以确保完整性。
他们的答案提供了相同的基本功能,但每个答案都是从另一个角度出发的。马特(Matt)的答案使用string.IsNullOrEmpty
-mentality,而马克(Marc)的答案则采用Linq的方式.Any()
完成工作。
我个人倾向于使用这.Any()
条路,但想从该方法的其他重载中添加条件检查功能:
public static bool AnyNotNull<T>(this IEnumerable<T> source, Func<T, bool> predicate = null)
{
if (source == null) return false;
return predicate == null
? source.Any()
: source.Any(predicate);
}
Run Code Online (Sandbox Code Playgroud)
因此,您仍然可以执行以下操作:
myList.AnyNotNull(item=>item.AnswerToLife == 42);
与常规操作一样,.Any()
但添加了null检查
请注意,使用C#6方法:myList?.Any()
返回a bool?
而不是a bool
,这是传播 null 的实际效果
这可能会有所帮助
public static bool IsAny<T>(this IEnumerable<T> enumerable)
{
return enumerable?.Any() == true;
}
public static bool IsNullOrEmpty<T>(this IEnumerable<T> enumerable)
{
return enumerable?.Any() != true;
}
Run Code Online (Sandbox Code Playgroud)
当涉及引用(可为空)类型并且项目中不存在空项目时,可以使用这一行进行验证
myCollection?.FirstOrDefault() == null
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
183807 次 |
最近记录: |