Tip 55 – How to extend an IQueryable by wrapping it.

Over the last couple of years I’ve found myself in lots of situations where I’ve wanted to get ‘under the hood’ and see what is happening inside an IQueryable, but I haven’t had an easy solution, at least until now. Getting down and dirty like this is interesting because it means you can: Log Queries…


Tip 54 – How to improve performance using Statement Expressions

Background: While writing the update post in my Data Service Provider series I ended up writing this block of reflection code to copy properties values from one object to another: foreach (var prop in resourceType           .Properties           .Where(p => (p.Kind & ResourcePropertyKind.Key)                           != ResourcePropertyKind.Key)) {       var clrProp = clrType            .GetProperties()           …


Tip 36 – How to Construct by Query

While writing my tips series and writing EF controllers for MVC I found that I regularly wanted to create and attach a stub entity. Unfortunately it isn’t quite that simple, you have to make sure the entity isn’t already attached first, otherwise you’ll see some nasty exceptions. To avoid these exceptions I often found myself…


Another C# trick: Fluents, Inheritance and Extension Methods

Here’s a scenario which Damien and I encountered recently. We needed a way of specifying Facets for properties on Entities, i.e. things like Nullable, MaxLength, Precision etc. And we needed a class hierarchy because different types of properties have different sets of available facets. The base class of the hierarchy is called PropertyConfiguration which defines…


A silly C# trick from the trenches

UPDATE: as Joe rightly points out in the comments (thanks Joe) when you try to pass a Func<T,V> to a method expecting Action<T> using lambda syntax it doesn’t fail. Somehow the compiler must realize there is no match for Func<T,V> and look for a match for with Action<T> as a parameter. I don’t know how…


I never knew: C# method overloads can return different types

I came to C# from Java, which I’m pretty sure doesn’t (or maybe didn’t) allow this, so I was super surprised today when I learnt that C# method overloads can have different return types. So this class is completely valid: public class Foo {     public Foo Bar();     public string Bar(Foo foo); } For…


An enumeration of one?

Having something enumerable is the gateway to all LINQ’s loveliness. But sometimes you have just one object. So how do you make that enumerable? var people = new Person[] {person}; Not exactly hard huh? In fact you can simplify this even more, you can replace new T[] with just new [] like this: var people…


LINQ to Objects and Buffer<T>

I was debugging a problem a couple of days ago, when I learned something interesting. I had some code that looked like this: foreach(Employee e in manager.Manages) {     … } The manager.Manages property returned a custom implementation of ICollection<Employee> that lazy-loads it’s data from somewhere when enumerated. I wanted to change the code to…


Sample Oracle Provider for EF

Jarek has released his sample provider for Oracle here. In his post he talks about a few of the more notable challenges he had and how he got around them. If you are writing an EF provider this is gold!


Fantasy Soccer – part 3 – Unit of Measure

Today I spent a bit of time working on the data model for my fantasy soccer application. I don’t want to talk about the actual model today though, because I’m not really ready. What I want to do is talk about a problem that occurred to me, namely Unit Of Measure.  Any real sport fan…