Sunday, 29 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();
IEnumerable<Users> User = get();
var p = User.Where(m => m.int_UserId > 560);
foreach (var UserList in p)
     //   Console.WriteLine(UserList.int_UserId);


////////////////////////////Process 2////////////////////////

Stopwatch s2 = new Stopwatch();
IQueryable<Users> Userf = geti();
var pe = Userf.Where(m => m.int_UserId > 560);
foreach (var UserList in pe)
     //   Console.WriteLine(UserList.int_UserId);

Wednesday, 18 January 2012 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) {

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.

Tuesday, 17 January 2012

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)

Wednesday, 11 January 2012

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);

Sunday, 1 January 2012

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};
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


In this case it will return 100 if the sequence is empty.