Posts

Showing posts from March, 2012

Interesting fact about the LINQ to SQL Designer

If you never want to go and set the columns of a table to “Never” update check, then just add a timestamp column to the table, and it will do this for you. Pretty cool!

Optimizing searches through lists

I just finished completing a significantly complex piece of work, and thought I would pen a few words on how I improved the performance of this code. This will help me remember how I did this as well as help the novice programmer: This is how my initial code looked like: for (int i = 0; i < small_list .Count; i++) {       item = small_list [i];       result = (from p in big_list where p.ToLower().StartsWith(item.ToLower()) && String.Compare(p, item, true) != 0 select p).ToList(); } If the size of the small list is “S” and the size of the big list is “B”, every time the code will go through “B” items to get the result. I optimized this code so that I use a dictionary to lookup the results of the search as shown below:         for (int i = 0; i < small_list.Count; i++)         {                 item = small_list[i];                 if (lookup.ContainsKey(item))                 {                     result = lookup[item];                 }             } The dictiona

Miscellaneous Ideas!

How to start a Task in C# which takes parameters: Task .Factory.StartNew(() => Method(arg1, arg2, ...)); How to handle ambiguity in code: You may have a situation where you need to add a parameter to a method in the code which is called from 1000 places. And you do it, and you look at a calling method and realize that you have no idea about what value to pass for the new parameter. What you do in this case, is to add the new parameter as a parameter to the calling method as well. Keep on adding the parameter in the caller method until you reach a point where it makes sense to provide a value, or the user is entering the value. You will always reach a point where you will be able to provide a proper value, as long as what you are doing made sense to begin with. How to handle a crying toddler who does not want to you to brush his teeth: Give him your toothbrush and let him brush your teeth while you brush his. He will be having fun while you get the work done!