c# - 将 List<Guid> 转换为 List<Guid? >

标签 c# linq casting

将 List 转换为 List

以下不编译:

public List<Guid?> foo()
{
    List<Guid> guids = getGuidsList();
    return guids; 
}

最佳答案

这个问题似乎改变了几次,所以我会用两种方式展示转换:

转换 List<Guid?>List<Guid> :

var guids = nullableGuids.OfType<Guid>().ToList();
// note that OfType() implicitly filters out the null values,
// a Cast() would throw a NullReferenceException if there are any null values

转换 List<Guid>List<Guid?> :

var nullableGuids = guids.Cast<Guid?>().ToList();

关于c# - 将 List<Guid> 转换为 List<Guid? >,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16212699/

相关文章:

c# - 如何用派生类序列化基类

c# - 在 linq 查询中使用表达式

c# - System.Linq.Dynamic 错误 类型 'StringComparison' 中不存在属性或字段 'Person'

java - 我想知道java中一行的含义

Java 将接口(interface)转换为类

c - 如何抑制 “warning: cast to pointer from integer of different size”?

c# - C# WPF 中的页面、框架、导航窗口

c# - 如何在不丢失数据的情况下减少整数数组(二进制数据 [0,1])的长度?

c# - Linq to Xml 查询

c# - 如何使 LINQ 表达式返回到类中?