Using Linq's Where/Select to filter out null and convert the type to non-nullable cannot be made into an extension method

瘦欲@ 提交于 2020-02-05 03:54:43

问题


Suppose I have

List<MyObject?> list = ...;

I want to turn it into List<MyObject>, but I have not been able to drop the nullable reference.

Below is an MCVE. In my project I have nullable reference warnings turned to errors, so the commented out line below will not compile.

If I do .Where(e => e != null).Select(e => e!) then it will be fine in the latest .NET Core 3.1.100, however I cannot extract this into an extension method.

I tried adding this extension method

    public static IEnumerable<T> NotNull<T>(this IEnumerable<T> enumerable)
    {
        return enumerable.Where(e => e != null).Select(e => e!);
    }

However it will not convert IEnumerable<MyObject?> to IEnumerable<MyObject> and I am unsure why. This leads me to an error like:

[CS8619] Nullability of reference types in value of type 'List' doesn't match target type 'List'.

Is there a way I can make the NotNull function above work somehow?


回答1:


You have to update your extension method to the following

public static IEnumerable<T> NotNull<T>(this IEnumerable<T?> enumerable) where T : class
{
    return enumerable.Where(e => e != null).Select(e => e!);
}

The point here is that you are converting the IEnumerable of nullable references to not nullable ones, therefore you'll have to use IEnumerable<T?>. where T : class generic constraint is needed to help the compiler distinguish between nullable reference type and Nullable<T> struct, as you can read here

Because of this issue between the concrete representations of nullable reference types and nullable value types, any use of T? must also require you to constrain the T to be either class or struct.

After that the following lines will be compiled without any warnings

var list = new List<MyObject?>();
IEnumerable<MyObject> notNull = list.NotNull();


来源:https://stackoverflow.com/questions/59431558/using-linqs-where-select-to-filter-out-null-and-convert-the-type-to-non-nullabl

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!