0

I'm writing a C# application that will synchronize data from a remote database to my MySQL web application, and am in need of advice on how to do this better.

In my C# app, I establish a connection to the remote MySQL database and pass that connection to a function which runs a loop to pass a list of queries, one in each iteration, to another function that executes each query. The way it is written works great, but it seems to be counter-productive with the use of the foreach loop and the IEnumerable function. I'm not sure if the way it is written now makes sense and I would like to know if I can write this without the foreach loop, while keeping the IEnumerable function, if possible.

private void UpdateRecords(MySqlConnection connection) {
    foreach (string query in UpdateQueries(connection, _queries)) { }
}
private IEnumerable<string> UpdateQueries(MySqlConnection connection, List<string> queries) {
    List<string>.Enumerator enumerator = queries.GetEnumerator();
    while (enumerator.MoveNext()) {
        MySqlCommand command = new MySqlCommand();
        command.Connection = connection;
        command.CommandText = enumerator.Current;
        command.ExecuteNonQuery();
        yield return enumerator.Current;
    }
}

EDIT: Added 4 lines to the UpdateQueries function describing the use of the MySQL connection in this function.

5
  • 1
    Why are you passing the connection to UpdateQueries? Why not just ditch that whole thing and just do foreach (var query in _queries) { }? Commented Jun 14, 2017 at 16:22
  • I don't get the point of UpdateQueries , the UpdateRecords code just seems equivalent to foreach (string query in _queries) { /* proccess query */ } Commented Jun 14, 2017 at 16:23
  • @ChrisPickford I editted the post to show why the connection is being passed to the UpdateQueries function. Commented Jun 14, 2017 at 16:29
  • My point still stands; you're reinventing the wheel with UpdateQueries. See answer below on how you could better structure this. Commented Jun 14, 2017 at 16:30
  • @apokryfos That's what I'm not sure about as well. The UpdateQueries function doesn't iterate without the foreach, which I understand is redundant. I'm only including UpdateQueries for learning purposes. Commented Jun 14, 2017 at 16:31

2 Answers 2

3

_queries itself is enumerable (because it is a List<string> which implements IEnumerable<string>, so you can rewrite this to just be:

private void UpdateRecords(MySqlConnection connection) 
{
    foreach (string query in _queries) 
    {
        // process query
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the suggestion, @RB. So in this case, would you recommend I completely remove all IEnumerable functionality, since _queries already implements IEnumerable<string>? The only reason I had it in there was for learning purposes, but it seems to not make very much sense in this situation.
Yes - you can just remove it. You would only need to implement a function like that if you had some sort of collection that didn't implement IEnumerable itself, or if you wanted to do something complicated like read sensor data produced in realtime or something
-1

I'm not sure why you are using Enumerator. You can just foreach the List

private IEnumerable<string> UpdateQueries(MySqlConnection connection, List<string> queries)
{
    foreach (var query in queries)
    {
        MySqlCommand command = new MySqlCommand();
        command.Connection = connection;
        command.CommandText = query;
        command.ExecuteNonQuery();
        yield return query;
    }
}

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.