[英]how can manipulate the different kind of array (string, int, char) as same format such as string in C#?
I have a collection of objects that each object would contains a different kind of array (string[],int[],char[]) or even single value as different types. 我有一个对象集合,每个对象将包含不同类型的数组(string [],int [],char [])或什至是单个值作为不同类型。
I want to get the values of each array as single format such as string and convert it to a comma text value. 我想以字符串等单一格式获取每个数组的值,并将其转换为逗号文本值。
here is the code that manipulates an object of collection as "UInt16[]" 这是将集合对象操纵为“ UInt16 []”的代码
UInt16[] arrCapabilities = (UInt16[])(queryObj["Capabilities"]);
foreach (UInt16 arrValue in arrCapabilities)
{
Console.WriteLine("Capabilities: {0}", arrValue);
}
You could try using the following code: 您可以尝试使用以下代码:
IEnumerable array = queryObj["Capabilities"] as IEnumerable;
if(array != null)
{
foreach(var item in array)
{
Console.WriteLine(item.ToString());
}
}
else
{
Console.WriteLine(queryObj["Capabilities"].ToString());
}
您可以使用LINQ将数组转换为IEnumerable字符串:
var myStrings = from c in arrCapabilities select c.ToString();
To Expand on Daniels answer and get the csv string you are after: 在Daniels答案上扩展并获取您要使用的csv字符串:
IEnumerable array = queryObj["Capabilities"] as IEnumerable;
if(array != null)
{
var csvString = String.Join(", ", array.Cast<object>().Select(x => x.ToString()));
Console.WriteLine(csvString);
}
else
{
Console.WriteLine(queryObj["Capabilities"].ToString());
}
Does this help? 这有帮助吗?
void WriteToConsole<T>(IEnumerable<T> items)
{
foreach (var item in items)
Console.WriteLine("Capabilities: " + item);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.