Zeeshan Hirani

  • How To Find Which Control Raised PostBack

    There are times in asp.net when I really want to know which control in the page raised caused the page to post back. An easy way to find this is to register an event with the control that raises PostBack and cast the sender property to the appropriate control as shown below.

  • Delay Loaded Property

    I recently discovered while playing with the linq OR designer that each property on a class has a property called DelayLoaded. It allows you to delay the loading of that column from the database until the user accesses that property on the object. Only when you access that property is when linq to SQL server makes a fetch to the database to get that column value. It comes handy in scenarios where you storing XML data type or pictures in a database column which you don't really need until user request for the data. Here is how the screen shot looks like.

  • Creating Private Auto-Implemented Property

    I have been using auto implemented property for a while but did not know that you can control the acessor for getter and setter. Basically I can use public at the property level and set private or protected at either getter or setter. I cant set acessor at all 3 levels meaning I cant have public defined at the property level, than set protected at the getter level and set private at the setter level. The acessor defined at the property level must be inherited by at least either getter or setter and the one left over can be separately defined as follows.

  • Comparing Anonymous Types

    C# 3.0 allows you to create anonymous types which is basically a handy way to use a class without defining a class. It is really a nice feature when you are building linq queries which does not return fixed number of columns depending on the query you write. When you write an anonymous class, compiler in the background creates a auto generated class with those properties. If you define two anonymous types with the same property name and data type and in the same order, compiler will only create 1 anonymous type in the background and it will reuse that auto generated class in both those anonymous declarations.However if the definition of the anonymous type changes compiler will generate a new class. Because of the reuse of class, you have the ability to compare to generic classes having the same structure. Not only can you compare two anonymous classes but can also do assignment when two anonymous classes have the same structure. Let's have a look at an example that illustrates this behavior.

  • Using Default Values in Generic Method

    There are times when you want to assign default values to your variables. For instance if you have a reference type variable the default value will be null. If it is a value type you could assign 0. But how do you check in a generic method if the value the user has passed in is a default value or not. The reason being every type has its own default value, you cant check against null because the user may have passed in value type which cannot be checked against null value. What if the user has passed in datetime data type, in that case the default value would DateTime.Min.

  • How To Use Profiler with Linq To SQL Queries

    SQL Profiler is one of the very good tools from SQL server products tools that can help you evaluate the kind of SQL statements Linq to SQL is sending to the database. Although datacontext provides you with a log property that allows you to output the SQL statement that gets send to the database, SQL Profiler is other option that helps you understand and see the SQL statements that gets executed.

  • How To do In and Like clause in Linq To SQL

    In clause is one of the common querying feature we use in SQL. But there is no clear explanation of how to write a Linq query which allows you to filter the results based on certain items in a list. Well after digging in through some of the query operators I found that there are various operators that you can use to tell a Linq query to filter the items based on items in a list. Let's have a look at few of those examples.

  • vacation time!!

    Ok guys, I am going on vacation for a week. Not sure if i would get time or enough strength to come back at night and write something from my hotel room. Yeah i am taking my laptop on vacation, i just can't stay away from computer for too long. I haven't gotten tired of blogging as yet. Hopefully i will force myself to do it enough that it becomes a habit that i get addicted to!!

  • ThenByDescending Operator Part 16

    ThenByDescending is one of the query operators available in linq. You can use ThenByDescending operator with linq to objects,linq to xml and linq to SQL.ThenByDescending operator is used when you want to sort by more than 1 column or property. You initially start with either orderby or orderbydescending. Since both these operators return IOrderedEnumerable<T>, you cannot reuse these operators again. This where ThenByDescending operator comes into play. ThenByDescending is an extension method which takes a keyselector delegate that returns the key which is used to sort the collection in descending order. The output collection returned from the ThenByDescending operator is of type IOrderedEnumerable<T>. Therefore if you want to sort again in descending order, you can reuse the same operator.

  • OrderByDescending Operator Part 15

    OrderByDescending is one of the ordering query operators available in linq.You can use OrderByDescending operator with linq to objects, linq to XML and linq to SQL. OrderByDescending operator can be used in method or dot syntax as well as query syntax which is more of a SQL like syntax.