Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
CommandBehavior.SchemaOnly
Message
De
30/07/2013 12:51:34
 
 
À
30/07/2013 12:42:09
Information générale
Forum:
ASP.NET
Catégorie:
Code, syntaxe and commandes
Versions des environnements
Environment:
C# 4.0
OS:
Windows 7
Network:
Windows 2003 Server
Database:
MS SQL Server
Application:
Web
Divers
Thread ID:
01579375
Message ID:
01579459
Vues:
43
This message has been marked as a message which has helped to the initial question of the thread.
>>>>>>>>Hi Viv,
>>>>>>>>
>>>>>>>>If I change definition of the TableSchema class to be
>>>>>>>>
>>>>>>>>public class TableSchema 
>>>>>>>>{
>>>>>>>>
>>>>>>>>internal String TableName;
>>>>>>>>
>>>>>>>>public List<ColumnSchema> Columns = new List<ColumnSchema>();
>>>>>>>>
>>>>>>>>}
>>>>>>>>
>>>>>>>>then how would I retrieve column schema for particular column name ?
>>>>>>>
>>>>>>>
var columnSchema = Columns.Where(x => x.ColumnName == "Name").FirstOrDefault();
>>>>>>
>>>>>>Good idea! So, you think it's better than using dictionary, right?
>>>>>>
>>>>>>If so, I'll try to change to your code and use a List.
>>>>>
>>>>>Don't see much point in a Dictionary here. And IEnumerable might be better the List....
>>>>
>>>>A Dictionary should give close to O(1) look up time, whereas an IEnumerable will be O(n). Depending on the size of the list and how often its used, the speed difference is probably negligible. It is easy enough to create the Dictionary though:
>>>>
Dictionary<string, ColumnSchema> columnTypes;
>>>>
>>>>            using (SqlDataReader reader = command.ExecuteReader(CommandBehavior.SchemaOnly))
>>>>            {
>>>>
>>>>                 columnTypes = (from DataRow r in reader.GetSchemaTable().AsEnumerable()
>>>>                                                  select new ColumnSchema
>>>>                                                      {
>>>>                                                          ColumnName = (string) r["ColumnName"],
>>>>                                                          ColumnSize = (Int32) r["ColumnSize"],
>>>>                                                          DbType = (SqlDbType) r["ProviderType"],
>>>>                                                          DataType = (Type) r["DataType"]
>>>>                                                      }).ToDictionary(cs=> cs.ColumnName);
>>>>            }
Fair point. Guess we don't know enough about the context in which the collection will be used. One downside of a Dictionary is that it makes it more difficult to access an item by anything other than the ColumnName?
>>
>>But in this context, I don't think the dicitonary is needed. A simple IEnumerable should suffice.
>
>I think Rob's right in that if there are a lot of lookups by ColumnName then Dictionary would be faster. OTOH is you wanted, say a list ofcolumns of int Types then IEnumerable would be better....
var intColumns = columnTypes.Values.Where(cs => cs.DataType == typeof(int));
Seems simple enough. The downsides to the dictionary are going to come in the additional time and storage needed to create the keys.
Précédent
Répondre
Fil
Voir

Click here to load this message in the networking platform