0
string[] B = C.OfType<object>().Where(o =>o  != null).Select(o => o.ToString()).ToArray();

I try to Convert Array C to string[] but, Array C have a lot of null and I hope null change to " " What Should I do?

1
  • try this string[] B = C.OfType<string>().Where(o =>o != null).Select(o => o.ToString()).ToArray(); Commented Apr 21, 2017 at 17:36

2 Answers 2

2

You don't need OfType<object>(), simply call string.Concat:

var s = string.Concat(C.ToArray());

If you look at documentation:

The method concatenates each object in args by calling the parameterless ToString method of that object; it does not add any delimiters. String.Empty is used in place of any null object in the array.'

Sign up to request clarification or add additional context in comments.

Comments

0

This should work:

var B = C.OfType<string>().Select(o => o ?? " ").ToArray();

.OfType<string>() will filter out all non-string values from C.

.Select(o => o ?? " ") will select the value, or " " if the value was null.

and .ToArray() will take the IEnumerable and turn it into a string[].

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.