IEnumerable <string> and string []
Is there any advantage to using this
private static IEnumerable<string> GetColumnNames(this IDataReader reader) { for (int i = 0; i < reader.FieldCount; i++) yield return reader.GetName(i); } instead of this
private static string[] GetColumnNames(this IDataReader reader) { var columnNames = new string[reader.FieldCount]; for (int i = 0; i < reader.FieldCount; i++) columnNames[i] = reader.GetName(i); return columnNames; } This is how I use this method
int orderId = _noOrdinal; IEnumerable<string> columnNames = reader.GetColumnNames(); if (columnNames.Contains("OrderId")) orderId = reader.GetOrdinal("OrderId"); while (reader.Read()) yield return new BEContractB2CService { //.................. Order = new BEOrder { Id = orderId == _noOrdinal ? Guid.Empty : reader.GetGuid(orderId) }, //............................ Both approaches are completely different, so it depends on what you are going to do with the result, which I would say.
For instance:
The first case requires the data reader to remain open until the result is read, the second does not. So, how long are you going to keep this result, and do you want to stay open for reading data for a long time.
The first case is less effective if you are definitely going to read the data, but probably more effective if you often do not, especially if there is a lot of data.
The result from your first case should only be read / repeated / searched once. Then the second case can be stored and searched several times.
If you have a large amount of data, then the first case can be used in such a way that you do not need to enter all this data into memory at one time. But then again, it really depends on what you are doing with IEnumerable in the calling method.
Edit: Given your use case, the methods are probably pretty much equivalent for any given kindness measure. Tables do not have a large number of columns, and your use of .Contains ensures that data will be read every time. Personally, I would stick with the array method here, if only because it is a simpler approach.
What is the next line of code ... looking for a different column name? If so, then the second case is the only way to go.
From my head: the version of the array means that you have to spend time creating the array. Most code clients may not need a specific array. Basically, I found that most of the code is just going to iterate over it, in this case, why waste time creating an array (or a list as an alternative) that you really don't need.
The first is lazy. That is, your code is not processed until iterates over the enumerable ones and because you use closures it will work the code until it gives a value then turn the knob back to the call code until iterates over to the next value via MoveNext . Alternatively, with linq, you can achieve the second by calling the first and then calling ToArray . The reason you can do this is to make sure that you get data like this when you make a call versus when you iterate if the values ββchange between them.
One of the advantages of using memory. If FieldCount says 1 million, then the last one needs to allocate an array with 1 million records, and the first does not.
This advantage depends on how you use the method. For example, if you process a list of files one by one, then there is no need to know all the files in front.