Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
CommandBehavior.SchemaOnly
Message
From
30/07/2013 12:42:09
 
 
To
30/07/2013 12:37:45
John Baird
Coatesville, Pennsylvania, United States
General information
Forum:
ASP.NET
Category:
Coding, syntax and commands
Environment versions
Environment:
C# 4.0
OS:
Windows 7
Network:
Windows 2003 Server
Database:
MS SQL Server
Application:
Web
Miscellaneous
Thread ID:
01579375
Message ID:
01579456
Views:
40
>>>>>>>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....
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform