Archives

Archives / 2008 / July
  • Using AsQueryable With Linq To Objects And Linq To SQL

    AsQueryable is a method that allow the query to be converted to an instance of IQueryable. When you use AsQueryable operator on an existing query and apply further transformations such as applying a filter or specifying a sort order, those lambda statements are converted to Expression trees. Depending on the provider you are using,  expression trees will be converted into the domain specific syntax and than executed. In the case of Linq to SQL provider, the expression tree would be converted to SQL and executed on SQL server. However if you use AsQueryable operator on a query that does not implement IQueryable<T> and only implements IEnumerable<T>, than any transformations that you apply on the query would automatically fall back on IEnumerable<T> specification. What this means is by tagging a query with AsQueryable you get benefits of both Linq to SQL and Linq to object implementation. If your existing query happens to implement IQueryable, the query is converted to SQL by the Linq to SQL provider, otherwise the query is executed in memory in the form IL code.

  • Handling Errors in Asp.net Ajax using ScriptManager

    Asp.net Ajax offers variety of ways to handle error that occur when the page is posted back asynchronously. Every time an error occurs in an asynchronous page request, script manager will raise an onasyncpostbackerror event. If you need to customize the error that get sent to the user, you can register with the event and set the a friendly error message on AsyncPostBackErrorMessage property available on ScriptManager. Code below shows an example where I am setting a friendly error message on the AsyncPostBackErrorMessage instead of displaying the exact error thrown on the server side code.

  • TODO comments in Vs 2008 service pack 1

    I have been using TODO comments in visual studio since vs 2003. It helps me keep track of all the items that I eventually want to get to but don't have the time to do it on time to meet my release date.  You can access the todo list from the view menu and clicking Task List. One of problems I had in the past with todo list was, it only showed todo tasks for files that are opened in visual studio. In a big project, it is not feasible to open all the files to see all the the todo tasks for the entire solution. In vs 2008 service pack 1, C# IDE team finally got around to fixing this issue. Now you get todo list for all the todo tasks in your solution regardless if the file is open or not.

  • Navigating Quickly in Vs 2008 Service Pack 1

    In the past, to navigate around bunch of files opened in vs 2008, I tend to use Cntrl + Tab to reach a particular file. It certainly is a good option but when you have large number of files opened in the project, you end up spending enormous amount of time just tabbing through the list of files until you get to the one that you want to open. Recently I discovered that when you use Cntrl + Alt + down array key, a small navigation window pops up at the top. The windows allows you to navigate to all the files opened. The best part about this window that I really like is, you can actually start type in the name of the opened file that you want to reach to and visual studio will automatically highlight the file that matches the name you are typing. Below is a screen that demos the feature.

  • Creating Hello World With Silverlight

    I finally have to embrace Silverlight as my team decided to use silver in building part of our community site. I had been trying to avoid Silverlight since 1.0 version, feeling that my Ajax skills and asp.net concepts would be all I need to do my daily activities. But with the upcoming release of Silverlight which supports .net runtime, world is changing over. We no longer have to program in JavaScript and can be comfortable writing code in our domain specific language such as C# or Vb.net. Today, I decided to begin my journey to learn Silverlight. The best way to learn any technology is to create a hello world application. Below is a step by step tutorial to create a hello world application.

  • Combining Expression Trees

    Today, I was working with expression trees and had the need to combine two expression trees. It appears that when you try to combine two expression trees, it doesn't work. Compiler raises an an exception and does not allow combining two expression trees. Instead you need to convert expression tree into delegate by compiling the expression tree and using the compiled delegate with the existing expression tree to get the new expression tree. Here is an example that C# compiler does not allow.

  • Modifying Expression Trees

    As I have started to play more with expression trees, I decided to learn how to modify expression trees. Well expression trees cannot be modified because they are read only. In order accomplish modification, you have to create a brand new tree, copy the existing expressions that are still valid, replacing the expression that you want changed.  Below is an example that demonstrates changing a constant value from 1 to 3.

  • Sorting Child Collections In Entity Framework

    I made a blog post earlier, where I discussed how to use AssociateWith method on DataLoadOptions to sort child collections on SQL server before they are loaded into memory. This option is only available in linq to SQL. In entity framework, in order to accomplish sorting on child collection, you have to use CreateSourceQuery method. CreateSourceQuery returns an objectquery that gets converted to SQL and send to the database. So if you want your child collections to be loaded differently such as sorted in a different order or apply filter, than you have to get access to ObjectQuery instance that is responsible for loading the child subcollection and modify the query to add ordering before ObjectQuery gets executed.  CreateSourceQuery query method is available on EntityCollection and EntityReference.

  • Ordering Lazy Loaded Child Collections on the database

    I have been working with OR mappers for more than 3 years and had the luxury to work with many different ones such as Wilson, NHibernate, Sonic and Linq. One of the constraints that I have encountered in most ormappers is, there is no clean way to define how to sort child collections based on certain column. For example if I have a customer instance in my hand and I want to get access to its Orders, I can simply navigate to Orders property of the customer. What if I want those orders to be sorted by ShipCity. Well in Linq queries you can apply OrderBy operator on Orders collection for the customer. But does that order by operation gets executed on SQL server. The answer is no. As soon as we access the Orders property of the customer, Linq to SQL fetches all the orders for the customer in memory. From there on any operations you perform will get executed in memory. Below is an example that confirms the behavior.

  • Returning Random Products Using Linq To SQL

    Today at work, I was given a requirement to randomize our featured products on the home page. Basically every time you reload the page, you get different set of products. The maximum number of random products you could display on home page must not be more than 20. I could have done the implementation in C# by bringing more products than I need and randomly picking up 20 from the list. Once I implemented the solution, my results were truly not randomized. I ended up with same records quite often because the randomizer function did not have enough products to randomize and plus I was bringing more data than I needed.  Meaning to randomly pick 20 products, I had to bring 100 products and apply random function on there. After having done that I realized that if I were to do the randomizing on SQL server I truly would get a random behavior. SQL server has a function called NewID that always give you a random generated number. So if I can order my collection by the random generated Id, my top 20 collection of products would always give me random 20 products from products table. Here is the view and a function that returns a random Id.

  • Building Expression Trees from Scratch

    Just out of curiosity i decided to create expression trees from scratch instead of using lambda statements to get my expressions trees. In all my projects, I haven't really found any significant need to create expression trees from scratch. Most of the time lambda statements are sufficient for me to create any complex expressions. However to understand lambda expressions better, it think it is essential to comprehend, how compiler converts lambda statements to expression syntax. In the example below, I am going to create a very simple expression tree that takes two parameters, multiples both parameters and adds two it.

  • Different ways of removing elements from XML

    Linq to XML provides numerous ways of modifying XML which includes adding, updating and deleting nodes. I happen to work on an XML document from which I had to remove some xelments. Surprisingly I found numerous options that facilitate deleting of various xelements from  XML loaded in memory. Depending on where you are in the XML tree, you might find one method easier than the other. Here is a simple example that illustrates various ways I discovered of removing xelements from XML loaded in memory.

  • XElement.ToString returns encoded text

    I am sure people who do XML on a day to day basis would not find surprising but I think it was pretty cool to see that when I call  ToString on my XML, I get back my entire XML with whitespace preserved and also all text is encoded for me automatically. I created my XML using .net 2.0 api and saw the same behavior so I guess this was not a new feature but definitely a good default option to encode your text before outputting it to the user. Here is simple example that illustrates this behavior.

  • Avoid impurities in Linq To SQL

    This problem actually troubled me for hours until I accidentally figured out how to get around it. What I learned is try to avoid impurities in Linq statements. For instance, if you lambda expression's filter is applied by indexing an array value, do not access the array directly inside the lambda value. Instead get the value from the array assign it to a variable and use the variable in the Linq to SQL filter statements. I am not sure why I have to declare a variable to avoid an impurity. But if you do not follow this technique, more than likely you would end up with runtime error in your Linq statement. Even if you get pass the runtime error, you would end up with a wrong filter value being applied on your lambda expressions. Let's take a look at an example to understand the problem I am trying to describe.

  • Applying aggregates to empty collections causes exception in Linq To SQL

    I spent nearly two hours trying to figure out what was wrong with my Linq to SQL query. Basically I was trying to calculate the sum of sales generated by each sales agent. In order to accomplish that I had to travel from employee table to Orders which had the employeeid linked to it. From the Order table I navigated to its order details and calculated the sum of Quantity ordered multiplied by Unit Price. The query works fine when each agent has put in an order in the database. But when there is no order record for an agent the sum operation performed on SQL server returns null. When Linq to SQL tries to assign null values to data types that is not defined as null, it throws an exception. Below is an example that demonstrates this issue.

  • Entity Refs Not getting serialized with WCF service

    In one of the project that I am working on, my client requires me to use WCF service to talk to Linq To SQL datacontext. The infrastructure guys would not open up port for me to access SQL server directly. Therefore I created a WCF project in my solution, added a reference to Business library and exposed my Linq to SQL entities using WCF service. By default you cannot expose your Linq to SQL entities, you have to mark SerializationMode on the datacontext to Unidirectional. Once you configure the datacontext for wcf serialization, the generated entities are marked with DataContract attribute and properties on the entity are marked with Datamember attribute. If Linq to SQL entity have child entities those also get serialized. However child entities do not get lazy loaded, you have to explicitly call LoadWith to immediately load child collections for them to be serialized. Here is a simply example that illustrates the behavior.

  • Automatic entityset mapping in Entity Framework

    As I am starting to do work on both linq to SQL and linq to entities, I am finding lots of subtle differences on both implementations. Some implementations I would consider as being better over the other.

  • Eager Loading child entities in Entity Framework

    Once again I am comparing different behaviors in linq to SQL and entity framework. If you want to eagerly load child entities in Linq to SQL, you would use LoadWith operator available on DataLoadOptions. LoadWith method retrieves related objects for the main object. You have the option to call LoadWith method as many times as you need depending upon how many related objects you want to eagerly load.  If you want to eagerly load related objects in entity framework, you you can call include method. Include takes a path which represents the name of the related entity that you want to load. In the path parameter you can also use traversal by using dot notations. For instance for a particular customer, if you want to load its Orders and for each order want to load its OrderDetails, instead of making of two Include calls you can call include with a traversal like cust.Include("Orders.OrderDetails"). This tells entity framework to load Orders and for each Order load its OrderDetails as well. As with linq to SQL, you also can make more than 1 calls to Include to load multiple entities at the same time. Below is an example that illustrates different usage of eager loading with entity framework and linq to SQL.

  • Lazy Loading child entities in Entity Framework

    It's pretty interesting to see different implementations of lazy loading in both linq to SQL and entity framework. In linq to SQL, if you want to access the Orders collection of a particular customer, all you do is navigate to the Orders Collection Property and linq to SQL will run the SQL in the background and fetch the orders for that customer instance. The operation is pretty implicit. However in entity framework you have to explicitly tell that I intent to load the orders of the customer. The way you would do this is by calling Load on the Orders collection of the customer. If you forget to call load on the Orders Collection for the customer, you would end up with an empty collection of orders. This may be confusing and hard to troubleshoot bug and you would wonder why you have no orders for that customer when you could see orders in the database for the customer. Here is an example that illustrates this behavior.

  • Not All Lambdas can be converted to expression trees!

    It was surprising for me to discover that depending upon how you write your lambda syntax, it may not be able to convert to expression tree. If you are using a body expression in your lambda statements meaning you don't have an explicit return statement and no parentheses, than the lambda expression can be converted to expression tree. However lambda expression with statement body which requires an explicit return statement can only be converted to delegates. Expression trees are important because most providers of linq take expression trees and convert them to their domain specific language where as delegates gets compile into IL code which only Linq To Objects provider can understand. This is why it is important that your lambda statements are convertible to expression trees. Here is an example below which illustrates a lambda  statement that cannot be converted to expression tree and the error compiler raises in regards to that.

  • Different Ways of representing Lambdas

    Today I had some free time at work since my batch process was taking an hour to run. During this time, I was investigating the beauties of lambda expression. I have found lambda expressions to be very flexible and versatile. What I mean is, your lambda expression can be as long and as small as you need it. If you find that in certain portion of your code, it would be much more easier to read if there is less noise from language, than you can use type inference. Places where you feel the code would be easier to read if there is more explicit types declared, should have proper parentheses and return statement than by all means using statement syntax  form of lambda expression. If you find the lambda expressions getting polluted by too much code and thus making it harder to read than by all means point the lambda expression to a method that does all the complicated logic. Here are different ways I have been able to express lambda statements.

  • Compiling Extension Methods on .net 2.0 runtime

    Today, I had to work on a legacy .net 2.0 application and I found a neat little trick one of developers did to get extension methods to work on .net 2.0 runtime. I was pretty amazed because I thought extension methods was a feature available in System.Core assembly that is part of .net 3.5 framework.  In reality that is completely true and Extensions are available only in .net 3.5. However extension methods gets converted into the same IL code that is compatible with .net 2.0 runtime. So the only thing left is to satisfy the Visual Studio compiler that extension methods are valid statements for .net 2.0. On asking the developer who wrote the code, he said that in Visual studio .net 2.0 compiler only checks for an attribute called System.Runtime.CompilerService.ExtensionAttribute to be available for extension methods to work. So he basically added a dummy class of that type that inherits from Attribute class. After adding the ExtensionAttribute class, the compiler was satisfied and the project build fine.

  • Design Time Compilation in Vs 2008 Sp1

    I have been playing around with Vs 2008 sp1 bits and one of the very cool debugging improvements I have noticed is, you don't have to compile the project to see compilation errors. By default C# compiler would put squiggly lines below the code that is causing the problems and when you hover over the squiggly lines it will show the exact error that you would get if you were to compile the project. I think this feature offers a tremendous developer productivity. I cant imagine how many times have I actually compiled the project to see what error the compiler gives me. But now with this feature I will save so much time. All I have to do is hover over the lines that is causing the problem I will get the same error as if I was compiling the project. Hats off to C# team in pulling off this feature. Screen shot below shows this feature in action.

  • Paste XML as Element

    XML in Vb 9.0 has become a first class citizen. You can copy XML content and assign it to Xelment variable and compiler will not complain at all. You can even get intellisence on the XML in Vb if you import XML namespace. C# does not provide all these goodies. But recently when I had to do XML parsing from the data returned by Google mini, I came across this smart paste option that I had not discovered before. Basically you can copy some XML content, go to view menu and select paste XML as Element and it will generate all the XML element code needed to create that markup. This gives you a good start in building up the XML data that you desire. Below is a screen shot that outlines the code generated when I selected Paste XML as Elements in Visual Studio.

  • Source Outline PowerToy For Visual Studio

    I recently came across this power toy add in For Vs 2008 on CodePlex and I must say that its powerful and really makes navigating across methods, searching for methods fairly non trivial. Once you install PowerToy on your machine, you can open it by going to View Menu, Other Windows and clicking Source Outliner Power Toy. This will open up Source outliner as you can see in the screen shot. If you are on a class, that class will be selected by default on Source Outliner and by clicking up and down arrows you can see Visual studio moving the code editor to that method without a breeze. You also have the ability to search and Filter for a method in a class as well entire solution and Outliner will start populating the list as you type. I think Source Outliner is a great tool to navigate and find methods quickly. You can download the tool at

  • Finding All references of a method in Vs 2008

    Once again I am sharing some of the productivity features i have been discovering as I am starting to play more and more with Vs 2008. Not sure if this feature that i am about to discuss was already there is vs 2005 or is simply new to vs 2008. Anyway, in a large code base when I are modifying a method, I am always curious to know how many places in the code this method is being used and to be able to breeze through all the references for the method and get an idea of the context its being used. Pressing SHIFT + F12 will open up reference list window that show you all the reference links where this method is being called. Simply pressing F8 would allow you to navigate to each reference of the method in the list.  Below is a screen shot that shows the feature.

  • Code Definition Window in VS 2008

    Once again I came across a hidden gem inside vs 2008 called Code Definition window. It really is a slick window that lets me see the definition of the class without actually traveling or navigating to the class. Let's say you are inside of class that has method called SaveCustomer which takes customer object. Lot of the times, if I want to see the full definition of the customer class I end up going to the class, looking through it and than have to find my way back to the method I was originally trying to debug. With Code Definition window, I can stay at the method that I am interested and simply but my cursor on the Customer object parameter and in the code window I can easily see the entire definition of the class. Below is a screen shot that demonstrate this feature.

  • Auditing linq to SQL entity classes

    There are many ways to implement auditing on your entity classes. If you are not exposing linq to SQL entities to the presentation layer and prefer to use linq as a data access layer, than you can applying auditing on the linq entities in your business layer. The reason is, you have more control over the persistence and business layer is responsible for retrieving linq entities as well as saving them. However if you have considered that you can get away with using linq entities in your presentation layer because it provides partial classes where you can implement custom business logic than linq to SQL provides various easy ways to implement auditing features.

  • Eager Loading Self referencing table in Linq to SQL

    Today at my work, I had the need to display category and all the nested categories with in it so that its much easier for Google to have a once place where it can follow all the links to our site. I am not by any means SEO expert but our SEO expert said that Google would seem to come back quite often if it can find hard coded links to our categories as compared to crawling query strings on a page.

  • Accessing private properties and methods without Reflection

    This blog post title might be very surprising to the viewers. I am sure people would be thinking how in the word can you access private properties or methods without using reflection. Well in your code you don't have much choice except to use reflection. However if you are debugging, than immediate window lets you access private level properties and method. I thought it was pretty slick and also meant easier and more productive debugging.  In the code below, I have an Employee class which has a private method and a private property. I created an instance of my class, than stopped the debugger in the next line, opened up the immediate window and ran the commands below. Sure enough immediate window allowed me to call the private attributes of a class although intellisence didn't seem to help any in terms of calling private stuff.

  • DataContext.GetChangeSet to find what objects have changed

    When you make changes to object, delete or insert new objects, linq to SQL is tracking the changes and based on the original values, it generates the appropriate SQL statements. DataContext exposes a method called GetChangeSet that contains all the inserted, updated and deleted objects that will be sent to the database when SubmitChanges gets called. You basically have to loop through each collection and find out what objects have been effected.  If you want to know exactly what SQL statement would be sent to the database their is a private method called GetChangeText that you can call using reflection to find out the exact SQL statement that would be sent to the database. Example below shows a change set that contains few updated objects, an inserted object and an object marked for deletion.

  • Lazy Loading Properties in Linq To SQL

    Linq to SQL provides deferred loading of foreign key object such as EntityRef and EntitySet so that you get the best performance and do not end up loading the entire object graph until you need it. However there are situations when you have binary or varchar(max) columns on a table which you do not want to load until you access the property. A real world example for this scenario would be like you are storing images in the product table or product table has description column which is set to varchar(max).  If all you are doing is querying the product table to bring  product collection and wont be touching description or image column than there really is no need to be loading those column ahead of time. If you are going to be using the Visual Studio designer than simply go to the property and set the Delay Loaded to true to turn on Delay Loading. On changing the setting in the properties window, Linq to SQL designer will create a private variable of type System.Data.Linq<T> where T is the type of property  like string, int etc. Here is an example where I have marked description column to be lazy loaded.