在Nullable <int>列表上使用Enumerable.OfType会删除空项</int>

时间:2014-01-25 02:00:05

标签: c# linq

考虑代码:

public class Foo : List<Nullable<int>> //could also say List<int?>
{
}

public void code()
{
  Foo foo = new Foo();
  foo.Add(null);
  foo.Add(1);
  foo.Add(2);
  foo.Add(3);

  DoSerialize(foo);

}

void DoSerialize(object obj)
{
  Type type = obj.GetType();
  if (ReflectionUtils.DoesTypeSupportGenericInterface(type, typeof(IList<>)))
  {
    IList<object> arrayList = null;
    arrayList = ((IEnumerable)@obj).OfType<object>().ToList(); //problem line
    foreach (object element in arrayList)
    {
      Serialize(element);
    }
  }
}

带有//问题行评论的行正在产生意外结果。 在DoSerialize()方法中,obj变量的类型为Foo,其中包含4个元素。 在ToList()调用之后,其中只有3个元素,包含值null的元素已被删除。

如何将Nullable<int><int?>的列表转换为对象列表,并使用null条目将列表基数维持在4?

2 个答案:

答案 0 :(得分:5)

使用Cast<object>代替OfType<object>

var source = new List<int?> { 1, 2, null, 4 };

var castCount = ((IEnumerable)source).Cast<object>().Count();  // returns 4
var ofTypeCount = ((IEnumerable)source).OfType<object>().Count();   // return 3

这是因为您将列表转换为非通用IEnumerable,因此int?会在object属性中投放到IEnumerator.Current。而(int?)null强制转换为object会返回null。然后,我的OfType<object>执行了item is object检查,null返回false

来自 Immediete Window

((object)((int?)null))
null

null is object
false

答案 1 :(得分:0)

信用到期;这是Phillip Scott Givens的建议。我认为值得发布一个代码示例。我想在此处添加您不需要ReflectionUtils:

void DoSerialize(object obj)
{
  var enumerableObject = obj as IEnumerable;
  if (enumerableObject != null)
    foreach (object element in enumerableObject)
      Serialize(element);
}