将List<指南>转换为List<指南的最佳实践是什么?>
以下代码无法编译:
public List<Guid?> foo()
{
List<Guid> guids = getGuidsList();
return guids;
}发布于 2013-04-25 18:53:34
像这样的东西
return guids.Select(e => new Guid?(e)).ToList();发布于 2013-04-25 18:56:50
这个问题似乎改变了几次,所以我将从两个方面展示转换:
将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();发布于 2013-04-25 18:47:14
public List<Guid> foo()
{
return foo.Where(x=>x != null).Cast<Guid>().ToList();
}https://stackoverflow.com/questions/16212699
复制相似问题