C#LINQ:从数组中删除空值并返回非空值 [英] C# LINQ: remove null values from an array and return as not nullable

查看:69
本文介绍了C#LINQ:从数组中删除空值并返回非空值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将可为空的数组转换为不可为空的数组.这是我当前的代码,其中包含两个函数调用:

I'm converting nullable array to unnullable. This is my current code with two function calls:

myarray.Where(e => e.HasValue).Select(e => e.Value)

这看起来是非常基本的操作.一次通话就可以做到吗?

It looks like a very basic operation. Is it possible to do that in one call?

推荐答案

您始终可以进行自己的扩展,但是仅会使您的代码看起来更简洁,请认为您的实现是最简洁明了的你可以得到说实话

You can always make your own extensions but that only makes your code seem more succinct, think that your implementation is the most succinct and clear you can get to be honest

public static IEnumerable<T> GetNonNullValues<T>(this IEnumerable<Nullable<T>> items) where T: struct
{
    return items.Where(a=>a.HasValue).Select(a=>a.Value);
}

这篇关于C#LINQ:从数组中删除空值并返回非空值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆