You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何将List转换为IEnumerable?求修复对象List提取整数函数

Extract Integers from a List<object> - Solution

Hey there! Let's work through this problem together. It sounds like you're trying to write a function that pulls integers out of a List<object> but you're stuck on converting the list to IEnumerable and getting the function to run correctly—let's fix that right away.

The Simplest Approach: Use OfType<int>()

The easiest way to get only integer values from your List<object> is using LINQ's OfType<T> method. This method does two things: it filters the collection to only include elements that are of type int, and it automatically casts those elements to int for you. Plus, since List<object> already implements IEnumerable<object>, you don't need to do any manual conversion to IEnumerable—it's ready to use out of the box.

Here's the function:

using System.Collections.Generic;
using System.Linq;

public IEnumerable<int> GetIntegerValues(List<object> objectList)
{
    // OfType<int> filters and casts in one step
    return objectList.OfType<int>();
}

What If You Need to Handle Convertible Values?

If your list has elements that aren't strictly int but can be converted to one (like strings with numeric values, long values that fit in an int, etc.), you can use a more flexible approach with type checking and parsing:

using System.Collections.Generic;

public IEnumerable<int> GetConvertibleIntegerValues(List<object> objectList)
{
    foreach (var item in objectList)
    {
        // Check if the item is already an int
        if (item is int intValue)
        {
            yield return intValue;
        }
        // Try parsing string values to int
        else if (item is string strValue && int.TryParse(strValue, out int parsedInt))
        {
            yield return parsedInt;
        }
        // Convert long values that fit within int range
        else if (item is long longValue && longValue >= int.MinValue && longValue <= int.MaxValue)
        {
            yield return (int)longValue;
        }
        // Add more cases here if you need to handle other types (e.g., double)
    }
}

Why Your Previous Attempt Might Have Failed

If you were trying to use Cast<int>() instead of OfType<int>(), that's likely the issue. Cast<int>() will throw an exception if it encounters an element that can't be cast directly to int, whereas OfType<int>() simply skips those elements. That's probably why your function wasn't running correctly.

Test It Out

Here's how you can test these functions with a mixed list:

var mixedObjects = new List<object> { 42, "not a number", 99, "123", 1000L, 3.14 };

// Get only strict int values: returns 42, 99
var strictIntegers = GetIntegerValues(mixedObjects);

// Get all convertible values: returns 42, 99, 123, 1000
var allConvertibleIntegers = GetConvertibleIntegerValues(mixedObjects);

That should solve your problem! Let me know if you run into any specific edge cases you need help with.

内容的提问来源于stack exchange,提问作者Yaroslav Dokuzov

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 08:13:48