I have a Data Row. I can get the items in it using the property dataRow.ItemArray
which is of type object[]
.
I need to convert this to String[]
or List<String>
I see the methods ToArray<>
and ToList<>
. but dont know how to use it.Kindly help.
Thanks in Advance
We can convert Object to String in java using toString() method of Object class or String. valueOf(object) method. You can convert any object to String in java whether it is user-defined class, StringBuilder, StringBuffer or anything else.
Pass the List<String> as a parameter to the constructor of a new ArrayList<Object> . List<Object> objectList = new ArrayList<Object>(stringList);
toList()) can be used to collect Stream elements into a list. We can leverage this to convert object to a list. //Similarly, you can change the method in the map to convert to the datatype you need.
You have two options depending on the actual objects in dataRow.ItemArray
If there are actually string objects in the object[] you can just cast element wise.
dataRow.ItemArray.OfType<string>().ToList();
but if the objects are of another type, like int or something else you need to convert the to string (in this example with .ToString()
but another custom method might be required in your case
dataRow.ItemArray.Select(o => o.ToString()).ToList();
Edit:
If you don't need List<string>
or string[]
explicitly you can leave the .ToList()
out and get an IEnumerable<string>
instead.
object[] a = new object[10];
string[] b = Array.ConvertAll(a, p => (p ?? String.Empty).ToString())
(the line you want is the second)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With