0

I have a database with some dates, i want to fetch them and convert them into a string[] to use in a graph.

        var Yaxis = db.Graphs
                    .Where(x => x.Node.Contains(Node))
                    .Select(x => x.Dates)
                    .ToArray();


        var data = new string[Yaxis.Length];
        for (int i = 0; i < Yaxis.Length ; i++)
        {
            data[i] = Yaxis[i].ToString;
        }

The .ToString do not work here with a "non-delegate type"-error. I dont know any other way to parse the array to a string[].

Any idea?

1
  • 1
    ToString is not a delegate, "ToString()" is a method :) Commented Feb 13, 2013 at 6:52

2 Answers 2

4
data[i] = Yaxis[i].ToString();

When you don't include the parentheses, the compiler will assume you're talking about the function ToString, when you're actually talking about the return value.

When you're talking about the function itself, it's called a delegate (kind of similar to a function pointer in C/C++), which explains the error you're getting.

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

5 Comments

@Habib No, it exactly addresses the problem. Look at the last three characters.
@Habib: No it's not - the brackets are missing in the question. That's the important bit, although Antonijn should include text explaining it.
@Habib, this answer contains parenthesis for the ToString() method. So, the answer is correct ....
oh yes, my bad :) but you should have added the details as well +1
Thank you Antonijn, sometimes you stare yourself dumb on the code :)
2

As Antonijn posted, the immediate problem is that you're using ToString without actually calling the method.

However, you can do better than this to start with by doing it in LINQ:

var data = db.Graphs
             .Where(x => x.Node.Contains(Node))
             .Select(x => x.Dates.ToString())
             .ToArray();

Note that we're calling ToString() in the projection here. If that doesn't give the result you want (e.g. because it performs the conversion in the database) you can split it into two Select calls, with an AsEnumerable call forcing the second one to execute locally:

var data = db.Graphs
             .Where(x => x.Node.Contains(Node))
             .Select(x => x.Dates)
             .AsEnumerable()
             .Select(x => x.ToString())
             .ToArray();

This will use the default string representation of DateTime in the current culture, of course. You may want to consider specifying a standard or custom date/time format string to change the output format, and maybe even a different culture... it depends on what you're going to do with the data.

All of this assumes that you don't need Yaxis for anything else. If you do need Yaxis, you can still use LINQ to simplify your code:

var data = Yaxis.Select(x => x.ToString()).ToArray();

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.