2

I have the following line of code:

List<int> _people = code.data.loadScalarDataPoint(code.enums.pathNames.Department, "Department_Staff", RecId).Split('|').Select(int.Parse).ToList();

The static method loadScalarDataPoint returns a string of the selected column for given inputs.

In this instance, it returns a list of pipe delimited integers (e.g. 12|45|88|1543|123) or if the column is NULL it will return an empty string.

Using the linq Select(int.Parse) works if there is a result but without it throws the following error "Input string was not in a correct format"

I know why, as you can't parse an empty string into a int, however is there a way within the single line of code to check for this?

Or do I need to get the result into a string, check if it has contents and if so parse into a list of ints?

5
  • 1
    There is something wrong with your setup - new List<string>().Select(int.Parse).ToList() works perfectly fine. Please consider providing actual sample that shows the problem. Possibly Nick Stuart's answer explains the problem (if you in fact have non-empty array). Commented Jan 24, 2015 at 2:39
  • 1
    "".Split('|').Select(int.Parse); would work but if the value before .Split is null, there would be an exception. The only time that the reported exception should show is when your string is actually not convertible to an int. Commented Jan 24, 2015 at 2:45
  • Alexei, the sample does show the problem, that the array consists of a single element array which is an empty string as the answers have provided. I can't see what else could be added, as there's the full exception as well. Commented Jan 24, 2015 at 5:04
  • @RemarkLima check out title of the post - ".Select(int.parse) on empty array..." - I'm not sure if you actually read it OR maybe your definition of "empty array" is different from mine (quite possible). I think it also would be much easier to see what exactly you have problem with if your sample is something like var r = "some string". Split(.... in addition to actual code you have. Commented Jan 24, 2015 at 6:43
  • @AlexeiLevenkov that is what my first diagnosis was, which as Nick Strupat has correctly diagnosed was incorrect and the problem was in that it was a single element array with an empty string. There is enough information in the question for an accurate answer. If you feel the title isn't going to help searches, please suggest an edit. Commented Jan 25, 2015 at 21:53

3 Answers 3

10

EDIT with full explanation: In your failing case, the loadScalarDataPoint method returns an empty string, which the call to Split(',') returns an IEnumerable<String> with one empty string in it. The call to Select(Int32.Parse) throws an exception because an empty string is not in the correct format.

Use

.Split(new [] {'|'}, StringSplitOptions.RemoveEmptyEntries)
Sign up to request clarification or add additional context in comments.

5 Comments

Would this not still result in an empty array trying to be parsed by the select linq?
Select doesn't do anything if the result set is empty.
I think you may have misdiagnosed your issue. The fact that Select(int.Parse) is doing anything at all suggests that the list returned by your call to Split('|') is returning an IEnumerable<String> which contains at least one empty string, which of course int.Parse will throw an exception on.
Nick, your diagnosis is correct but the code is wrong. It will need to be .Split(new string[] {"|"}, StringSplitOptions.RemoveEmptyEntries) as that overload of Split take a string array as the first argument.
@RemarkLima Good catch! I've made the appropriate edit.
2

why not check for non empty result first?

List<int> _people = code.data.loadScalarDataPoint(code.enums.pathNames.Department, "Department_Staff", RecId).Split('|').Where(a => a.Any()).Select(int.Parse).ToList();

2 Comments

I'll try it out, looks like it'd work, but perhaps would still result in an empty array trying to be parsed...?
I tried it before I posted the response. You will end up with an empty list of type int.
1

Add the following before the select:

.Where(x=>!string.IsNullOrEmpty(x))

This basically ensures that when doing the select there are only elements that have a string value.

Be aware you will need to handle the empty case.

2 Comments

Would this not still result in an empty array trying to be parsed by the select linq?
Nope - I actually tested it and the other response by Nick. They bother work. Building off of the .Split is probably the better solution in this case.

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.