Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/304.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#从选项列表中删除空值后列表的结果类型_C#_Linq_Optional - Fatal编程技术网

C#从选项列表中删除空值后列表的结果类型

C#从选项列表中删除空值后列表的结果类型,c#,linq,optional,C#,Linq,Optional,假设我有一个C选项列表(这里称为x),我通过删除空值创建了一个新列表(这里新列表称为y) static void Main() { var x=新列表{7,8,9,null,10,null,11}; 变量y=x,其中(e=>e!=null); Console.WriteLine(“{0}”,y); } 然后推断y的类型为List。获取列表类型的惯用方法是什么?显式展开还是显式转换?还是有更干净的方法 在我正在处理的实际实现中,值是作为选项列表出现的,我需要将这些列表转换为下游的集合,因此我不想

假设我有一个C选项列表(这里称为
x
),我通过删除空值创建了一个新列表(这里新列表称为
y

static void Main()
{
var x=新列表{7,8,9,null,10,null,11};
变量y=x,其中(e=>e!=null);
Console.WriteLine(“{0}”,y);
}
然后推断
y
的类型为
List
。获取
列表类型的惯用方法是什么?显式展开还是显式转换?还是有更干净的方法


在我正在处理的实际实现中,值是作为选项列表出现的,我需要将这些列表转换为下游的集合,因此我不想“推卸责任”到后续操作中去(尽管我可以在稍后的代码中进行大量的空值检查…yuk)。我真的想要一个longs变量列表。在C#中如何最好地考虑和处理这个问题?

我会检查
HasValue
属性并选择

你可以试试这个:

var y = x.Where(e => e.HasValue).Select(e => e.Value);

无论是否使用
.ToList()
只需使用
x.OfType().ToList()
-这将过滤掉所有空值,结果类型将为
列表

使用Select进行Explit cast将完成以下任务:

        var x = new List<long?> { 7, 8, 9, null, 10, null, 11 };
        var y = x.Where(e => e != null);
        Console.WriteLine("{0}", y);
        IEnumerable<long> z = x.Where(e => e != null).Select(e => (long)e);
        Console.WriteLine("{0}", z);
var x=新列表{7,8,9,null,10,null,11};
变量y=x,其中(e=>e!=null);
Console.WriteLine(“{0}”,y);
IEnumerable z=x.Where(e=>e!=null);
Console.WriteLine(“{0}”,z);

我认为最好是创建自己的扩展方法:

public static class Extensions
{
    public static IEnumerable<T> RemoveNullsAndConvert<T>(this IEnumerable<T?> source)
        where T : struct
    {
        return from x in source where x != null select x.Value;
    }
}
当然,您可以为方法使用较短的名称,并且实现不一定必须使用LINQ—例如,它可以是一个C#迭代器:

public static class Extensions
{
    public static IEnumerable<T> RemoveNullsAndConvert<T>(this IEnumerable<T?> source)
        where T : struct
    {
        foreach (var x in source)
            if (x != null) yield return x.Value;
    }
}
公共静态类扩展
{
公共静态IEnumerable RemoveNullsAndConvert(此IEnumerable源)
其中T:struct
{
foreach(源中的变量x)
如果(x!=null),则返回x.值;
}
}

虽然简洁,但在我看来,这是一种过分的做法,因为它涉及到不必要的装箱和拆箱。@IvanStoev这是一个非常有用的评论。很难在这个答案和Jeroen的答案之间做出决定,Jeroen的答案简洁地说“只保留所有的长度”,Jeroen的答案非常明确地说明了在存在未包装值的地方获取这些值!你对拳击的看法是对的,但对于示例中的列表,我甚至不会考虑它。无论如何,您可以始终使用
Where->Select
approach。
var y = x.RemoveNullsAndConvert();
public static class Extensions
{
    public static IEnumerable<T> RemoveNullsAndConvert<T>(this IEnumerable<T?> source)
        where T : struct
    {
        foreach (var x in source)
            if (x != null) yield return x.Value;
    }
}