C# 对象列表到字符串列表

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19436124/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 14:58:40  来源:igfitidea点击:

C# Object List to String List

c#objectcastingtype-conversion

提问by Melih Mucuk

object list

对象列表

I have List<object>that seems above, I want to convert it to List<string>.

我有List<object>上面的内容,我想将其转换为List<string>.

How can I convert it ?

我怎样才能转换它?

I need List<string>that has 6 items (11:00,13:45,.... etc)

我需要List<string>有 6 个项目(11:00,13:45,.... 等)

采纳答案by Alyafey

var mylist = myObjectList.ConvertAll(x => x.ToString());

Edit

编辑

  var mylist = myObjectList.ConvertAll(x => Convert.ToString(x));

thanks Scott Chamberlain

感谢斯科特张伯伦

To get first array of objects

获取第一个对象数组

var mylist = (myObjectList.First() as object[]).ToList()
                .ConvertAll(x=>Convert.ToString(x));

To add rest to the list.

将休息添加到列表中。

mylist.AddRange(mylist.GetRange(1,myObjectList.Count-2).ConvertAll(x=>Convert.ToString(x)));

回答by Alexandre Rondeau

You can simply cast it using LinQ.

您可以使用 LinQ 简单地投射它。

myObjectList.Cast<string>();

Or filter all non-string

或过滤所有非字符串

myObjectList.OfType<string>();

回答by TGH

var stringList = yourObjectList.OfType<string>().ToList();

Remember to add the namespace System.Linq;

记得添加命名空间 System.Linq;

The OfType is needed to convert the array to an array<T>which is necessary in order to use it with LINQ

需要 OfType 将数组转换为一个array<T>,以便在 LINQ 中使用它是必需的

回答by Santin

Casting like :

铸造如:

var list = (List<String>) listObjects.

回答by Sriram Sakthivel

Try this

尝试这个

List<string> stringlist = objectList.Cast<string>()
                                    .ToList();

If you're not certain about those elements are strings you can use Select

如果您不确定这些元素是字符串,您可以使用 Select

List<string> stringlist = objectList.Select(x=> x.ToString())
                                    .ToList();

To avoid NullReferenceExceptionin case of null values try the following

为避免NullReferenceException出现空值,请尝试以下操作

List<string> stringlist = objectList.Where(x=> x != null)
                                    .Select(x=> x.ToString())
                                    .ToList();

回答by Scott Chamberlain

Using LINQ this is fairly easy. If you are sure they are all strings you can simply do

使用 LINQ 这相当容易。如果你确定它们都是字符串,你可以简单地做

int i = //Some code that sets i, like a for loop
var oldList = seanceInfo.theatre[i].seances;
List<string> newList = oldList.Cast<string>().ToList();

If you are not sure all of the objects are strings you need to perform some kind of conversion, however that is just as easy

如果您不确定所有对象都是字符串,则需要执行某种转换,但这同样简单

List<string> newList = oldList.Select(o => Convert.ToString(o)).ToList();


From your comment: "seances is List<object>, and first index of seances is object[]. I need these items.", I think what you reallywant may be a SelectMany

从您的评论:“降神会是List<object>,降神会的第一个索引是object[]。我需要这些项目。”,我认为您真正想要的可能是SelectMany

List<string> info = seanceInfo.theatre.SelectMany(x => x.seances).Select(x => Convert.ToString(x)).ToList();

This will take each seance in each theater and combine it in to one master list.

这将需要每个剧院的每次降神会并将其合并到一个主列表中。