Dave Glick (Page 27)

Round Robin Row Selection From SQL Server

I've been trying to answer at least one question a day on Stack Overflow recently, and one came up yesterday that I thought was a pretty good little SQL problem: how can you efficiently select one row from a database in a "round robin" fashion? That is, how can you make sure the selections are evenly distributed? Turns out this can be accomplished with a single SQL query on SQL 2005 and newer using the OUTPUT clause. Assuming the table has an "Id" primary key and a "LastSelected" DateTime column, the following SQL query will select the record that hasn't been selected in the longest time (or pick an arbitrary one if there is a tie), update the last time that record was selected, and then return all columns for the record.

Read more...

Quick and Dirty Multiple Value Dictionary Using Extension Methods

Though it's not a collection I tend to reach for often, there have been times when I really need a multiple value dictionary (that is, a dictionary that contains more than one value per key). In the past, I've usually reached for the excellent PowerCollections library to fill the gap. However, that requires bringing in another library and it can be a little heavy-weight for just this one collection class. There are also a ton of other implementations out there. But perhaps there's a better way to fill this need, one that doesn't require a lot of extra code. These two extension methods do most of the work of a multiple value dictionary, but don't require any extra classes or libraries:

Read more...

Custom Entity Type Configurations in Entity Framework Code First (Part 2)

In my last post I discussed how to inherit from the EntityTypeConfiguration class and use reflection to dynamically configure Entity Framework. In this post I'll expand on that technique by using a custom interface, reflection, and several helper classes to automatically apply Entity Framework configurations from arbitrary classes.

Read more...