C# 将字符串转换为列表类型
我有一个列表方法,需要将枚举值作为列表类型返回。状态数据类型是枚举对象 这是我的密码:C# 将字符串转换为列表类型,c#,wpf,enums,C#,Wpf,Enums,我有一个列表方法,需要将枚举值作为列表类型返回。状态数据类型是枚举对象 这是我的密码: public List<string> Statusstring { get { foreach (Status stat in System.Enum.GetValues(typeof(Status)).Cast<Status>().ToList()) { stat
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() };
}
}
这是我收到的错误消息:
无法将类型“System.Collections.Generic.List”隐式转换为“System.Collections.Generic.List”
有人能帮我一下吗。如果你必须只返回列表,那么下面是一个黑客程序
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() };
}
属性名为StatusString,但它是一个列表?
我想下面的代码可能是正确的实现:
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();
}
}
}
或者,如果你必须返回一个列表,你可以试试这个
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());
}
}
}
要列出的第一个枚举
我看到的是一个简单问题的非常复杂的解决方案
private static List<string> StatusString() => Enum.GetNames(typeof(Status)).ToList();
但由于枚举值在运行时不会改变,所以我选择静态成员
可以定义静态成员→
private static List<string> StatusString = Enum.GetNames(typeof(Status)).ToList();
这应该可以
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
}
在.NET中有此的内置构造:
public IEnumerable<string> Statuses => Enum.GetNames(typeof(Status))
如果返回IEnumerable而不是列表,则可以将其分配给数组或列表。还有Enum.GetNames。。。以字符串[]形式返回
此方法的文档是如何:
Enum.GetValues(typeof(Status)).Cast<Status>()
将返回一个Ienumerable
Enum.GetValues(typeof(Status)).Cast<Status>().ToList();
您的方法签名指示您将返回列表,但字符串的.ToList返回列表。这条信息实际上解释了这一点。我猜也许这仍然不是你想要做的,但只有你知道你想要做什么。此外,您还可以访问.yes@John如何使其返回字符串而不是char?我一直在尝试很多方法,但都不管用。你在这里想做什么?您有一个while循环,但在第一个条目上返回。您还可能遇到另一个错误,即由于reader而未返回值。Read可能在第一次调用时返回false,而不会进入循环。您的意思是将每个字符转换为字符串并将其作为列表返回?Status.Disable.ToString.Selectc=>c.ToString.ToList,但我想这不是您想要的。当然,我不知道你想要什么。a为什么要对单个值使用组合框?b Prateek的答案将使您能够从单个值返回列表。使用此代码,它将打印我想要的值,但while循环中的return语句如何?@anonymous_apple由于您只需要单个值,请在查询中使用SELECT TOP 1,然后将while改为if语句。如果没有匹配的值,您还需要从方法返回一个值,例如空列表,例如返回新列表@anonymous_apple-如果你能分享一些关于阅读器和db对象类型的详细信息-也许我能为它找到一些东西。另一方面,当您在查询后期望得到多个结果时,通常使用while reader.read。为什么不直接执行var result=reader.read;检查结果是否为false,即不读取任何内容,否则您已经具有此if条件,但在我的组合框中,我希望显示启用和禁用选项,以允许用户select@PrateekShrivastava你能打开聊天窗口吗?在那里讨论对我来说比较容易。退货声明怎么样?我怎样才能归还stringlist?@EmrahSüngünice我刚看到你的答案。好主意。@anonymous_苹果看看我的答案,你可以让你的方法更通用一些,这样你就可以把它保存到数组或列表中。
public IEnumerable<string> Statuses => Enum.GetNames(typeof(Status))
Enum.GetValues(typeof(Status)).Cast<Status>()
Enum.GetValues(typeof(Status)).Cast<Status>().ToList();