Skip to main content

Posts

Showing posts from January, 2012

Using c# stopwatch class (System.Diagnostics Namespace).

If you want to compare two or more piece of code in your application for time taken this is the way to do it. Stopwatch s1 = new Stopwatch(); s1.Start(); IEnumerable<Users> User = get(); var p = User.Where(m => m.int_UserId > 560); foreach (var UserList in p) { // Console.WriteLine(UserList.int_UserId); } s1.Stop(); Console.WriteLine(s1.ElapsedMilliseconds); ////////////////////////////Process 2//////////////////////// Stopwatch s2 = new Stopwatch(); s2.Start(); IQueryable<Users> Userf = geti(); var pe = Userf.Where(m => m.int_UserId > 560); foreach (var UserList in pe) { // Console.WriteLine(UserList.int_UserId); } s2.Stop(); Console.WriteLine(s2.ElapsedMilliseconds);

Asp.net mvc 2 sending Json request to external url and returning value from it.

$(document).ready(function () { $("#Submit1").click(function () { var txtvalue= $("#txtvalue").val(); var txtvalue2= $("#txtvalue2").val(); var urlStr = "&txtvalue=" + txtvalue+ "txtvalue2=" + txtvalue2; $.getJSON("http://localhost:5555/Home/Index?format=json&callback=?" + urlStr, function (data) { alert(data); }); }); }); The important point of note here is that you will not be going to get back any value due to the restriction by the browser to stop the xss without specifying callback=? in the url which will result in JsonP call (not a plain JSON call) which is used for calling the external urls. Method which is going to serve the json request. public ActionResult Index(FormCollection form) { var data = new { Value ="true"}; return Json(data,JsonRequestBehavior.AllowGet); } Use jquery 1.5 or greater version.

How to Remove httpruntime Cache in c#

var enumerator = HttpRuntime.Cache.GetEnumerator(); Dictionary<string, object> cacheItems = new Dictionary<string, object>(); while (enumerator.MoveNext()) cacheItems.Add(enumerator.Key.ToString(), enumerator.Value); foreach (string key in cacheItems.Keys) HttpRuntime.Cache.Remove(key);

Linq syntax for sql's not in and in

For In here how it goes var Userlist = new List<Int32> { 560, 561, 511, 611 }; var User= context.GetTable<Users>(); var q = (from p in User where Userlist.Contains(p.int_UserId) select p); For Not In here how it goes var Userlist = new List<Int32> { 560 }; var User= context.GetTable<Users>(); var q = (from p in User where !Userlist.Contains(p.int_UserId) select p);

Using Linq DefaultIfEmpty() extension method

By Using DefaultIfEmpty() extension method you can check the sequence for empty and return something for it and do some manipulation on it afterwards. For example var numbers = new int[] {1,2,3,4,5,6,7}; Console.WriteLine(numbers.DefaultIfEmpty().Sum()); If the sequence is empty it will return 0 otherwise it will sum and return that. By Default if you want to return some value other then 0 it will return that as well Console.WriteLine(numbers.DefaultIfEmpty(100).Sum()); In this case it will return 100 if the sequence is empty.