简体   繁体   English

无法将类型为“WhereListIterator`1[System.Object]”的对象转换为类型“System.Collections.Generic.IEnumerable`1[System.Int32]”

[英]Unable to cast object of type 'WhereListIterator`1[System.Object]' to type 'System.Collections.Generic.IEnumerable`1[System.Int32]'

I'm trying to write function to get return all integers from object list and I keep getting: 'Unable to cast object of type 'WhereListIterator 1[System.Object]' to type 'System.Collections.Generic.IEnumerable 1[System.Int32]'.'我正在尝试编写函数以从对象列表中返回所有整数,但我不断收到:“无法将类型为“WhereListIterator 1[System.Object]' to type 'System.Collections.Generic.IEnumerable对象转换为“System.Collections.Generic.IEnumerable 1[System.Object]' to type 'System.Collections.Generic.IEnumerable 。 Int32]'.'

static void Main(string[] args)
    {
        var list = new List<object>() { 1, 2, "a", "b" };
        Console.WriteLine(GetIntegersFromList(list));
    }

    public static IEnumerable<int> GetIntegersFromList(List<object> listOfItems)
    {
        IEnumerable<int> ints = (IEnumerable<int>) listOfItems.Where(x => x is int);
        return ints.ToList();
    }

I tried casting it, not casting it, adding ToList() everywhere and I always get the Invalid Cast Exception.我尝试投射它,而不是投射它,到处添加 ToList() 并且我总是得到无效的投射异常。

The output should be: {1, 2}输出应该是:{1, 2}

Linq's Where() returns a WhereListIterator<T> , with T being the T of the source IEnumerable<T> , in your case still object . LINQ的Where()返回一个WhereListIterator<T>T作为TIEnumerable<T>你的情况仍然object

Either Cast<T> :要么Cast<T>

IEnumerable<int> ints = (IEnumerable<int>)listOfItems.Where(x => x is int).Cast<int>();

Or, shorter, use OfType<T>() :或者,更短的,使用OfType<T>()

IEnumerable<int> ints = listOfItems.OfType<int>();

If you're trying to write the integers to the console, you will need to convert the IEnumerable to a string :如果您尝试将整数写入控制台,则需要将IEnumerable转换为string

var list = new List<object>() { 1, 2, "a", "b" };
Console.WriteLine(string.Join(", ", list.OfType<int>()));

// Output: 1, 2

Or iterate over the IEnumerable :或者迭代IEnumerable

var list = new List<object>() { 1, 2, "a", "b" };
foreach (int i in list.OfType<int>()) Console.WriteLine(i);

// Output:
// 1
// 2

If you must implement GetIntegersFromList then you can just create a simple pass through:如果您必须实现GetIntegersFromList那么您可以创建一个简单的传递:

public static IEnumerable<int> GetIntegersFromList(List<object> listOfItems)
    => listOfItems.OfType<int>();

Or if you cannot use LINQ:或者,如果您不能使用 LINQ:

public static IEnumerable<int> GetIntegersFromList(List<object> listOfItems)
{
    foreach (var item in listOfItems)
    {
        if (item is int i) yield return i;
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法投射 <TakeIterator> d__3a`1 [System.Object]键入System.Collections.Generic.IEnumerable - Unable cast <TakeIterator>d__3a`1[System.Object] to type System.Collections.Generic.IEnumerable 错误“无法将'System.Int32'类型的对象强制转换为'System.Collections.Generic.List`1 [System.Int32]'' - error “Unable to cast object of type 'System.Int32' to type 'System.Collections.Generic.List`1[System.Int32]'” 无法将类型为“ System.Collections.ArrayList”的对象转换为类型为“ System.Collections.Generic.IEnumerable” - Unable to cast object of type 'System.Collections.ArrayList' to type 'System.Collections.Generic.IEnumerable' 将类型从System.Int32更改为System.Collections.Generic.IEnumerable - Change type from System.Int32 to System.Collections.Generic.IEnumerable IQueryable Union,无法创建类型为&#39;System.Collections.Generic.IEnumerable`1 [[System.Int32 - IQueryable Union, Unable to create a null constant value of type 'System.Collections.Generic.IEnumerable`1[[System.Int32 无法将类型为“ System.Data.Entity.Infrastructure.DbQuery`1 []”的对象转换为类型为“ System.Collections.Generic.IEnumerable”的对象 - Unable to cast object of type 'System.Data.Entity.Infrastructure.DbQuery`1[]' to type 'System.Collections.Generic.IEnumerable 无法将“System.Linq.OrderedEnumerable`2[***]”类型的对象转换为“System.Collections.Generic.IEnumerable`1[***]” - Unable to cast object of type 'System.Linq.OrderedEnumerable`2[***]' to type 'System.Collections.Generic.IEnumerable`1[***]' 无法将类型为“ System.Data.SqlClient.SqlDataReader”的对象转换为类型为“ System.Collections.Generic.IEnumerable” - Unable to cast object of type 'System.Data.SqlClient.SqlDataReader' to type 'System.Collections.Generic.IEnumerable' 无法投放“ <T> d__14`1 [System.Object]到&#39;IEnumerable`1 [System.Int32]&#39; - Unable to cast '<T>d__14`1[System.Object] to 'IEnumerable`1[System.Int32]' 无法将类型为“ System.IO.FileSystemInfo []”的对象转换为类型为“ System.Collections.Generic.IEnumerable`1 [System.IO.DirectoryInfo] - Unable to cast object of type 'System.IO.FileSystemInfo[]' to type 'System.Collections.Generic.IEnumerable`1[System.IO.DirectoryInfo]
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM