Zeeshan Hirani

  • Using External Mapping File With Linq To SQL DataContext

    By default when you use visual studio to drag tables onto Linq to SQL designer, you get table per entities generated in a single file. Those entities have attributes defined on them that defines how an entity is mapped to a table in the database. Although each of the generated entities are marked as partial, you never have to look at them because if you want to write code in those classes, you have the option to create your own partial class and put your logic in there. This makes your partial classes clean and devoid of any generated attributes which clutters up the code, making it hard to read. However using sqlmetal command line utility you have the option to move those attributes to an external mapping file and read the external mapping file at runtime as one of the parameters to the DataContext's constructor.  Below are some of the examples of different usages of sqlmetal that provides various flexibility in code generation and gives control in how the code is generated.

  • Is Your Linq Query being executed on the database?

    Depending on the approach you take when writing linq query, you may be surprised that some of the operations are getting performed in memory instead of the call being translated to sql and executed on the database. When you apply aggregate operators on association relationship on an entity in the context of a query syntax, the aggregations is performed on the database. However the same syntax when executed outside of the query syntax would force the entire association relationship to be brought from the database and aggregate operation performed in memory. In figure 30, I have two different version of the same query that returns the customerId, Total number of orders they have placed and TotalAmount they have spent on orders so far. In the first query, I am using association relationship Orders available on customer to get the total orders placed. To get the total amount spent, I first used the association relationship Orders on the customer and for each order, I navigate to its order Details association to calculate the cost of each Order. Since I am using association relationship inside of a linq query, the entire query is converted to sql and send to the database for execution. Second query in Figure 30, uses the same association relationship on the customer entity to perform calculations. Since the calculations are not part of an existing query, linq to sql has to bring all the orders for the customer and for each order bring down all its OrderDetails to calculate the Sum and Count operation. Bringing all the Orders and OrderDetails for a customer is an expensive operation which need not to be performed if all you are want ing to do is get the count and sum. Those operations can easily be done on the database. As a developer, it is important to understand the tradeoffs and know which option may be better suited for your scenario. For instance if you already have the order and OrderDetails for a customer in memory, than it may be more efficient to perform these operations in memrory instead of making a database call to the server.

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