|
-
June 20th, 2008, 10:54 PM
#11
Re: Problem with DataTable’s ‘Select’ method
The reason I recommend against using Select in most circumstances is because of the way it works... This happens AT RUNTIME when the select statement is encountered...
1) Evaluate the Expression, and generate SOURCE CODE that will evaluate the condition in terms of elements in a row.
2) Feed that source code to the Compiler, generate MSIL
3) Load the MSIL into an AppDomain.
4) JIT Compile the code.
5) Iterate over each row passing the row to the dynamically generated code.
EXTREMELY POWERFUL....EXTREMELY COMPLEX....HIGH POSSIBILITY FOR ERRORS (failure to load appdomain,. failure to invoke compiler....)....HIGH OVERHEAD (memory AND performance).
Therefore it should only be used where you can NOT code:
Code:
List<DataRow> result = new List<DataRow>();
foreach (DataRow row in Table.Rows)
{
if (/*write your own code here*/)
result.Add(row);
}
As long as you can fill in the comment, then this is MUCH preferred. However if you want to allow dynamic (unpredictible) input such as:
[CustomerNumber]>1000 && [Price]<49.99
or
[CustomerState]="NY" && [Quantity]>10
Then by all means use Select.
But if you KNOW the comparision is going to involve specific fields (compared angainst unknown limits) then a coded loop is much better.
TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
2008, 2009,2010
In theory, there is no difference between theory and practice; in practice there is.
* Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions 
* How NOT to post a question here
* Of course you read this carefully before you posted
* Need homework help? Read this first
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|