【问题标题】:Convert string to a List type将字符串转换为 List 类型
【发布时间】:2018-09-18 02:42:00
【问题描述】:

我有一个 List 方法,我需要将枚举值作为列表类型返回。 Status 数据类型是一个枚举对象。

这是我的代码:

public List<string> Statusstring
    {
        get
        {
            foreach (Status stat in System.Enum.GetValues(typeof(Status)).Cast<Status>().ToList())
            {
                status = stat;
            }

            return new List<string> { status.ToString() };
        }

    }

这是我的枚举值:

public enum Status
{
    Enable,
    Disable
}

这是我收到的错误消息:

无法将类型 'System.Collections.Generic.Listchar>' 隐式转换为 'System.Collections.Generic.Liststring>'

有人可以帮我解决这个问题吗?

【问题讨论】:

  • 你的方法签名表明你会返回List&lt;string&gt;,但是.ToList()的字符串返回List&lt;char&gt;。该消息实际上解释了这一点。我猜也许这仍然不是你想要做的,但只有你知道你想要做什么。此外,您对SQL injection 持开放态度。
  • 是的@John 如何让它返回字符串而不是字符?我一直在尝试很多方法,但对我没有用。
  • 你想在这里做什么?您有一个 while 循环,但您在第一个条目上返回。您可能还会遇到另一个关于不返回值的错误,因为reader.Read 可能会在第一次调用时返回 false 并且不会进入循环。
  • 您的意思是将每个char 转换为字符串并将其作为列表返回? Status.Disable.ToString().Select(c =&gt; c.ToString()).ToList(),但我希望这不是你想要的。当然,我不知道你做什么想要什么。 Example
  • a) 为什么要使用组合框来表示单个值? b)Prateek 的答案将使您能够从单个值返回列表。

标签: c# wpf enums


【解决方案1】:

.NET 中有一个内置结构:

public IEnumerable<string> Statuses => Enum.GetNames(typeof(Status))

如果您返回 IEnumerable&lt;string&gt; 而不是列表,则可以将其分配给数组或列表。 Enum.GetNames(...)string[] 的形式返回。

这个方法的文档是here

【讨论】:

  • @EmrahSüngü 很好,我刚刚看到你的回答。伟大的思想。
【解决方案2】:

如果你只需要返回列表 - 那么下面是一个#hack

                if (reader["PRODUCTION_LINE_CODE"].ToString() == "1")
                {
                    return new List<string> { Status.Enable.ToString() };
                }
                else
                {
                    return new List<string> {Status.Disable.ToString() };
                }

更新:- 与 OP 讨论后

if (reader["PRODUCTION_LINE_CODE"].ToString() == "1")
                {
                    return new List<string> { Status.Enable.ToString(), Status.Disable.ToString() };
                }
                else
                {
                    return new List<string> {Status.Disable.ToString() };
                }

【讨论】:

  • 用这段代码打印我想要的值但是while循环中的return语句怎么样?
  • @anonymous_apple 由于您只需要一个值,请在查询中使用SELECT TOP 1,并将while 更改为if 语句。如果没有匹配的值(例如return new List&lt;string&gt;();),您还需要从方法中返回一个值(例如空列表)
  • @anonymous_apple - 如果你可以分享一些关于阅读器和数据库对象是什么类型的详细信息 - 也许我能找到一些东西。附带说明一下,当您期望查询后有多个结果时,通常使用 while (reader.read())。为什么不直接做 var result = reader.read();检查结果是否为假(即没有读取),否则你已经有这个 If 条件
  • 但是在我的组合框中我想同时显示启用和禁用选项,以允许用户选择
  • @PrateekShrivastava 你能打开聊天窗口吗?我在那里讨论会更容易。
【解决方案3】:

我看到的是一个简单问题的非常复杂的解决方案。

private static List<string> StatusString() => Enum.GetNames(typeof(Status)).ToList();

但由于枚举值在运行时不会改变,我会选择静态成员

你可以定义一个静态成员→

private static List<string> StatusString = Enum.GetNames(typeof(Status)).ToList();

【讨论】:

  • @anonymous_apple 也看看我的回答,你可以让你的方法更通用一点,这样你就可以将它保存到数组或列表中。
【解决方案4】:

该属性被命名为Statustring,但它是List&lt;string&gt;? 我猜下面的代码可能是正确的实现:

public string Statusstring
{
    get
    {
        string query;

        query = "Select PRODUCTION_LINE_CODE from productionlineconfig where ID = '" + ProductionLineConfigs.ProductionLineId + "'";

        reader = db.QueryCommand(query);
        while (reader.Read())
        {
            if (reader["PRODUCTION_LINE_CODE"].ToString() == "1")
            {
                return Status.Enable.ToString();
            }
            else
            {
                return Status.Disable.ToString();
            }
        }
}

或者,如果您必须返回 List&lt;string&gt;,您可以试试这个

public List<string> Statusstring
{
    get
    {
        string query;

        query = "Select PRODUCTION_LINE_CODE from productionlineconfig where ID = '" + ProductionLineConfigs.ProductionLineId + "'";

        reader = db.QueryCommand(query);
        while (reader.Read())
        {
            if (reader["PRODUCTION_LINE_CODE"].ToString() == "1")
            {
                return new List<string>().Add(Status.Enable.ToString());
            }
            else
            {
                return new List<string>().Add(Status.Disable.ToString());
            }
        }
}

【讨论】:

    【解决方案5】:

    应该这样做。

    using System;
    using System.Linq;
    using System.Collections.Generic;
    
    public class Program
    {
        public static void Main()
        {
            List<string> list = System.Enum.GetValues(typeof(Status))
                .Cast<Status>()
                .Select(x => x.ToString())
                .ToList();
    
            list.ForEach(Console.WriteLine);
        }
    }
    
    public enum Status
    {
        Enable,
        Disable
    }
    

    【讨论】:

      【解决方案6】:

      首先列出enum

      var enumList = Enum.GetValues(typeof(Status))
              .Cast<int>()
              .Select(x => x.ToString())
              .ToList();
      

      然后将enumList 转换为字符串列表

      var stringList  = enumList.OfType<string>();
      return stringList;
      

      【讨论】:

      • return 语句呢?我怎样才能返回字符串列表?
      【解决方案7】:

      这个怎么样:

      Enum.GetValues(typeof(Status)).Cast<Status>() 
      

      将返回一个 Ienumerable。

       Enum.GetValues(typeof(Status)).Cast<Status>().ToList();
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-11-25
        • 2012-03-03
        • 1970-01-01
        • 1970-01-01
        • 2020-05-21
        • 1970-01-01
        • 2010-09-20
        • 1970-01-01
        相关资源
        最近更新 更多