LINQ
Be very careful when you need to recreate your entities with Linq to SQL. If I added a new column to the table and I needed to refresh my entity I used to remove it from the design and re add it again. This has worked for a while until now when all of a sudden I started getting errors in my production environment. What happened? I used to have the property DeleteOnNull on an association and when I removed the associated entity to recreate it it also removed the association. When I added the entity back to the diagram...
Anyone who has been trying to use LINQ To SQL with ASP.NET would know of the problems of dealing with state. Since LINQ To SQL does require to maintain state of the entities and the DataContext, the stateless behavior of the web leaves us with a lot of problems that we have to deal with. One of the patterns I have seen over and over again is to keep the entity in memory (through cache, session, viewstate, etc) during postbacks, create a new context, and reattach the entity to the new context. The problem with that is that the entity...
One of the cool features about Visual Studio is that you can extend it. One of the many things you can extend are the debugging Visualizers. While you're debugging an application with Visual Studio 2005 and 2008 you can hover over a variable and you can see it's value. Sometimes, depending on the type, you will see a magnifying glass icon and that icon is the Visualizer. The reason why sometimes you get a magnifying glass and sometimes you don't is because a specific type might not have a Visualizer associated with it. You are free to create your own...
For some reason I am inclined to write LINQ expressions using the Extension Methods. You know the one that looks something like this: Context.Products.Where(p => p.ProductID > 100).OrderBy(p => p.Description).ToList();
Well the other day I was in need to order by multiple columns and my first instinct was to use an && operator just like you do when you want to filter by more than one condition. Of course that did not work. After further investigation I found my answer. When you need to sort by multiple properties you have to use the ThenBy Extension method after using the OrderBy....
One of the great things about LINQ is it's deferred execution. Read about deferred execution here and here. What does this mean to us developers? Well it means many things, but one of those things is that you can dynamically construct your LINQ expressions based on some input. Let's say you have a search form and you want to add the filters only when they are selected. You can easily do this like this: var q = DB.Products.Where(p => p.Published); if (categoryID != null) q = q.Where(p => p.CategoryID == categoryID); if (!String.IsNullOrEmpty(description)) q =...
I was in need to order by a sort expression passed in to a function and since I am using LINQ to SQL the only supported way to do this is by having a switch clause and construct the orderby clause based on the sort input. This could result in a huge switch statement based on the total number of possible sort expressions. I wasn't very happy with that solution until I found this example. The DynamicQuery example is the sample I needed. In that example there is a Dynamic class which extends LINQ. These extension methods allows you to...