【问题标题】:System.InvalidCastException : Unable to cast object of type 'System.Int32' to type 'System.String' [closed]System.InvalidCastException:无法将“System.Int32”类型的对象转换为“System.String”类型[关闭]
【发布时间】:2021-10-25 02:17:36
【问题描述】:

任务是:给定一个正整数值序列integerList。 获取仅奇数 integerList 值的字符串表示序列并按升序排序 命令。 这是我的代码:

public static IEnumerable<string> Task5(IEnumerable<int> integerList)
    {
        var result = from item in integerList
                     where item % 2 != 0
                     orderby item descending
                     select item;
        return result.Cast<string>();
    }

但我每次尝试开始测试时都会看到此消息:System.InvalidCastException : Unable to cast object of type 'System.Int32' to type 'System.String'。

【问题讨论】:

  • 使用.ToString()方法代替Cast
  • 这能回答你的问题吗? IEnumerable to string delimited with commas?
  • 因为这看起来像是家庭作业,我建议借此机会学习如何阅读错误消息和使用调试器。如果您使用的是 Visual Studio,您可以attach a debugger 并运行代码,它应该会准确地告诉您问题出在哪里:result.Cast&lt;string&gt;() 表达式。然后您可以查找(单击并按 F12)Enumerable.Cast 方法,看看它为什么会抛出该异常。
  • 你的作业是否提到了如何处理任何重复的 [奇数] 号码?
  • 此外,这里的每个答案,包括发布的原始代码,都按 descending 顺序对值进行排序,但任务描述清楚地表明 ascending命令。 descending 可能应该从代码中删除

标签: c#


【解决方案1】:

不要强制转换,使用 .ToString() 代替

  public static IEnumerable<string> Task5(IEnumerable<int> integerList)
    {
        var result = from item in integerList
                     where item % 2 != 0
                     orderby item descending
                     select item.ToString();
        return result;
    }

【讨论】:

    【解决方案2】:

    result整数 的集合,而不是 字符串。而且您不能直接将整数转换为字符串。但是你可以打电话给他们.ToString()。因此,您可以使用 .Select() 之类的东西使用该操作将它们投影到新集合中。例如:

    return result.Select(r => r.ToString());
    

    尽管当源数据为整数时,此方法是否应该返回字符串集合可能值得研究。为什么不直接返回整数集合呢?看起来更干净:

    public static IEnumerable<int> Task5(IEnumerable<int> integerList)
    {
        return from item in integerList
               where item % 2 != 0
               orderby item descending
               select item;
    }
    

    很难确定,因为“Task5”并没有详细描述正在执行的操作。但是该方法是接收整数,看起来意图只是修改该集合,所以它会返回整数是有道理的。

    如果出于此方法范围之外的某些目的需要字符串,则由消费代码将它们转换为字符串。

    【讨论】:

      【解决方案3】:
        public static List<string> Task5(IEnumerable<int> integerList)
          {
              var result = from item in integerList
                           where item % 2 != 0
                           orderby item ascending
                           select item;
              List<int> mylist = result.ToList();
              return mylist.ConvertAll(x => x.ToString());
          }
      
      
          public static IEnumerable<string> Task5(IEnumerable<int> integerList)
          {
              var result = from item in integerList
                           where item % 2 != 0
                           orderby item ascending
                           select item.ToString();
              return result;
          }
      

      【讨论】:

        【解决方案4】:

        这听起来对我来说也绝对像是一个家庭作业……如果是这样,也许 Task5 这个名字是作业的要求。如果不是,那么正如@David 建议的那样,方法名称应该反映该方法执行的操作(例如FindOddNumbersAscending()

        以下是我认为基于作业描述的正确实施方式,也反映了上述反馈:

        public static IEnumerable<string> FindOddNumbersAscending(IEnumerable<int> integerList)
        {
            return from item in integerList
                   where item % 2 != 0
                   orderby item
                   select item.ToString();
        }
        

        这可以稍微进一步防止空输入以及删除任何重复项(如果需要的话;注意切换到流畅的语法而不是查询语法来删除重复项):

        public static IEnumerable<string> FindOddNumbersAscending(IEnumerable<int> integerList)
        {
            return integerList is null
                ? Enumerable.Empty<string>()
                : integerList
                    .Where(item => item % 2 != 0)
                    .Distinct()
                    .OrderBy(item => item)
                    .Select(item => item.ToString());
        }
        
        

        【讨论】:

        • 我还应该注意,我和@David 对将数字转换为字符串有同样的担忧。这绝对让人觉得应该关注一些消耗代码的事情,而不是这个操作的副作用。也就是说,这完全取决于分配/任务的参数。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2020-05-16
        • 1970-01-01
        • 2019-12-24
        • 1970-01-01
        • 1970-01-01
        • 2014-02-03
        • 1970-01-01
        相关资源
        最近更新 更多