Filtering Null values in Select

前端 未结 4 847
悲&欢浪女
悲&欢浪女 2020-12-08 18:06

I have IQueryable list of objects of type T which I want to transform into objects of type K

List tranformedList = originalList.Select(x => trans         


        
4条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-08 19:03

    What about

        List tranformedList = originalList
                                 .Select(x => transform(x))
                                 .OfType()
                                 .ToList()
    

    Takes care of unboxing an getting rid of nulls at the same time (especially when K is a struct)

    David B I dont believe you that your code .Where(y => y != null) works when K is an int! There is NO WAY you will get that code to compile if K is an int!

提交回复
热议问题