Click here to Skip to main content
16,018,294 members
Please Sign up or sign in to vote.
2.50/5 (2 votes)
in my project i implemented a service class which has a function naming GetList() which is as follows:

XML
IList<SUB_HEAD> GetList(string u)
{
    var collection = (from s in context.DB.SUB_HEAD where (s.head_code.Equals(u))
    select s);
    return collection.ToList();
}

drpdwn.datasource=Getlist();
drpdwn.databind();



which can also b implemented as
C#
Arraylist unitlist= new Arraylist();
ObjectSet<SUB_HEAD> List = subheadService.GetAll();
foreach(SUB_HEAD unit in List)
{
    unitlist.Add(unit.sub_head_code);
}

drpdwn.datasource=unitlist;
  drpdwn.databind();

Purpose of doing this is to populate dropdown menu.

My question is that "which of the above method will be more efficient with respect to processing?" because my project have lot of places where i have to use drop down menu.
Posted
Updated 3-Feb-14 8:59am
v3
Comments
Matt T Heffron 3-Feb-14 13:46pm    
These two code fragments are NOT equivalent.
The first one is comparing strings and the second is not.
Apples and Oranges...
Basit Elahi 3-Feb-14 15:01pm    
i know it is not same and for the same reason i have mentioned at the end that both of these are used to populate drop down list. Now i have improved the question. Please check out the latest one
Sergey Alexandrovich Kryukov 3-Feb-14 15:07pm    
What prevents you from timing both cases (after you make sure they give you identical results)? Overall performance may depend on different factors and be better for one technique in one case, and better for another technique for another case.
—SA
BillWoodruff 3-Feb-14 21:55pm    
I sometimes think a large percentage of QA questions should be answered like this, by suggestions for what the inquirer can DO to answer the question for themselves, perhaps with a link ... in this case, perhaps, a link to a how-to-time .NET operations CodeProject article.

As the old saying goes: "give a man a fish, and you feed him for a day; teach a man to fish, and you feed him for a lifetime."

But, then, that wouldn't be CodeProject ... would it ?
Sergey Alexandrovich Kryukov 3-Feb-14 22:01pm    
Absolutely! I already answered with this wise proverb after someone blamed me for providing not quite a complete solution.

Would it be CodeProject? I am quite sure it will. CodeProject is already like that. It's a mix of relatively bad and relatively good answers. Using the idea you just expressed (if we do it properly, depending on question and our understanding of the inquirer's background), we can only improve the ratio.

—SA

As a general rule of thumb I prefer for loop over foreach loop over LINQ; that is from Performance point of view.

See this[^] and this[^]

[Please encourage participation by up-voting solutions or answers that work for you]
 
Share this answer
 
v2
From the looks of your code, you are retrieving the menu content from the database and are concerned about performance.
If that is the case, then the best solution is to not make the call to the database.
Load the various dropdown menus from the database at startup and store the results in a dictionary.
The dictionary could be a Lazy loaded static application wide object.
Alternately, store the menus by key (u) in the cache and reload and cache if not in cache. This has the advantage of allowing you to set a cache timeout, forcing the menus to be refreshed on a regular basis, allowing for menu (database) changes without having to restart the app.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900