C# 无法将类型“字符串”隐式转换为“System.Collections.Generic.List<string>”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14654923/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Cannot implicitly convert type 'string' to 'System.Collections.Generic.List<string>'
提问by hsim
This question has probably been answered hundreds of times, but here goes.
这个问题可能已经回答了数百次,但在这里。
I have this chunck of code:
我有这段代码:
private void PopulateStringDropdownList(List<ObjectInfo> listObject,
object selectedType = null)
{
List<string> listString = listObject.OrderBy(x => x.m_Type).ToString();
for (int i = 0; i < listString .Count; i++)
{
for (int j = i + 1; j < listString .Count; j++)
{
if (String.Equals(listString [i], listString [j]))
{
listString.RemoveAt(j);
}
}
}
ViewBag.property1 = new SelectList(listString );
}
So basically I'm trying to populate a dropdown List with strings coming from a property of each objects contained in the list I pass in parameter.
所以基本上我试图用来自我传入参数的列表中包含的每个对象的属性的字符串填充下拉列表。
But the code doesn't compile because of the error you're seeing up there, and I've yet to understand why exactly. Any help anyone?
但是由于您在那里看到的错误,代码无法编译,我还没有完全理解为什么。任何人有帮助吗?
采纳答案by FishBasketGordo
This is the problematic line:
这是有问题的行:
List<string> listString = listObject.OrderBy(x => x.m_Type).ToString();
ToString
returns a string, and you're trying to assign to a List<string>
variable.
ToString
返回一个字符串,而您正试图分配给一个List<string>
变量。
You need to do something like this:
你需要做这样的事情:
List<string> listString = listObject.OrderBy(x => x.m_Type)
.Select(x => x.ToString())
.ToList();
This statement will order your listObject
enumerable to the order that your want, then convert the values to strings, and finally convert the enumerable to a List<string>
.
此语句将您的listObject
可枚举排序为您想要的顺序,然后将值转换为字符串,最后将可枚举转换为List<string>
.
回答by Matthew Strawbridge
I think you want ToList
instead of ToString
.
我认为你想要ToList
而不是ToString
.
Also, if all you're trying to do is remove duplicates, you can simply use LINQ and do this:
此外,如果您要做的只是删除重复项,则可以简单地使用 LINQ 并执行以下操作:
List<string> listString = listObject.OrderBy(x => x.m_Type).
Select(x => x.ToString()).Distinct().ToList();
回答by Jon Skeet
Look at this code:
看看这段代码:
List<string> listString = listObject.OrderBy(x => x.m_Type).ToString();
The right hand side expression is calling ToString()
on the result of OrderBy
- which isn't useful itself, and will result in a string
.
右手边的表达式正在调用-ToString()
的结果,OrderBy
这本身没有用,并且会导致string
.
You're then trying to assign that string
expression to a new variable of type List<string>
. That's not going to work.
然后,您尝试将该string
表达式分配给类型为 的新变量List<string>
。那是行不通的。
I suspectyou just want ToList
instead of ToString
:
我怀疑你只是想要ToList
而不是ToString
:
List<string> listString = listObject.OrderBy(x => x.m_Type).ToList();
回答by Cédric Bignon
You can simplify all your code into:
您可以将所有代码简化为:
List<string> listString = listObject.OrderBy(x => x.m_Type)
.Select(x => x.ToString())
.Distinct() // Remove duplicated entries
.ToList();
ViewBag.property1 = new SelectList(listString);
回答by hsim
There was still a bit of correcting to be done. Here's the complete code:
还有一些更正要做。这是完整的代码:
private void PopulateStringDropdownList(List<ObjectInfo> listObjects, object selectedType = null)
{
List<string> listString = listObjects.OrderBy(x => x.m_Type)
.Select(x => x.m_Type.ToString())
//.Distinct()
.ToList();
ViewBag.cardType = new SelectList(listString );
}
That way the correct value found is the string of the Type and not the type of the object.
这样找到的正确值是 Type 的字符串,而不是对象的类型。
Thanks everyone for your help!
感谢大家的帮助!
回答by Arvind Singh
Convert list of int
to list of string
:
将列表转换int
为列表string
:
List<string> listString = listObject.OrderBy(x => x.m_Type).
Select(x =>x.ToString()).ToList();