Archives

Archives / 2008 / April
  • Linq to SQL Debug Visualizer

    I have been learning Linq for sometime. However I always had some difficulty figuring out what SQL statement got generated for my Linq query. Although there are numerous ways to find the SQL statement that get generated but none of them were with out code or having to leave visual studio environment. Last week I discovered a really good tool called Linq to SQL Visualizer which allows me look at my Linq query in a Visualizer that exactly shows the SQL statement generated. It also allows you to execute your query from there as well.

  • 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.

  • ThenBy Operator Part 14

    ThenBy is one of the ordering query operators available in linq. You can use ThenBy operator with linq to objects, linq to XML and linq to SQL. You will typically use ThenBy operator to do secondary sort. ThenBy operator is used to sort by more than 1 property. When you want to sort initially you would start with using the orderby operator. Since orderby operator returns IOrderderedEnumerable<T>, you cannot reuse the orderby operator again because it can be only used on sequences that implement IEnumerable<T>.  This is where the ThenBy operator comes into play. ThenBy operator is an extension method that is available on input sequence that implement IOrderededEnumerable<T> and also returns IOrderedEnumerable<T> which allows you to reuse it multiple times if you want to sort by more than 1 column or property. The prototype of ThenBy operator looks like this

  • OrderBy Operator Part 13

    OrderBy is one of the ordering query operators available in linq. In terms of availability, you can use OrderBy query operator with linq to objects, linq to SQL and linq to XML. Unlike many query operators that are only available in method or dot syntax, OrderBy query operator can also be used in query syntax which is sometimes easier to use than method syntax.

  • Union Operator Part 12

    Union is one of the set query operators available in linq. It appends 2 input sequences together ensuring that duplicates are removed and returns a single output sequence. The prototype for union operator looks like this

  • Reverse Operator Part 11

    Reverse is one of ordering query operator. Reverse operator simply returns the input sequence in the reverse order. It does not effect the elements in the input sequence in any way. The prototype of the reverse operator looks like this

  • SequenceEqual operator Part 10

    Sequence operator determines if two sequences are equal to each other.Two collections are considered equal if they have the same number of elements with same values and in present in the same order.

  • Concat Operator Part 9

    Concat is one of the set operators available in Linq. When you work with Linq query operators for sometime, you would notice that there are certain query operators that can all perform a certain job but some operators in certain scenario are easier to work with or more efficient in performing the computation that others. Concat operator allows you to join two sequences together into one single output sequence. You can perform the same operation using selectmany or union operator. The selectmany operator has an advantage that it would allow you to join many sequences into one single sequence.Concat supports joining only two collections and therefore you would have to repeat this operation several times to achieve the same result that selectmany operator would give. Union offers the same functionality with little more power. With union, you can get rid of duplicates that is elements that are common in both sequences. Union uses the default EqualityComparer. If this comparer does not suffice you needs, you have an overloaded option to specify you own IEqualityComparer that defines how to compare two elements to identify if there are same or not.

  • SkipWhile Operator Part 8

    SkipWhile Operator skips elements based on the delegate condition. It continues to skip the elements as long as the predicate returns true. As soon as it encounters false statement, it returns rest of the elements. The prototype of the skip operator looks like this

  • Skip Operator Part 7

    Skip is one of the filtering operators available in linq. Skip operator skips the first count elements and returns rest of the elements in the collection. The prototype of the skip operator looks something like this

  • Take While Operator Part 6

    Takewhile is a partitioning operator available in linq. It starts from the beginning of the series and continues to return the elements from the input sequence as long as condition is validating to true. As soon as it hits the first statement that evaluates to false it skips rest of the collection.

  • Take Operator Part 5

    Take is one of the filtering operator available in linq. It returns first n number of elements and discards rest of the collection. The prototype of Take operator looks like this

  • Select Operator Part 4

    Select operator is one of the projection operator available in LINQ. Select operator allows you to return an output sequence of certain type by transforming the input sequence using the lambda expression. The output sequence could be of same element type or different but the number of elements returned from the Select operator is always same as the number of elements passed in the input sequence.