Entity Framework can save you a lot of time when developing data operations in your application.  I am going to demonstrate some techniques to use calculated fields to filter or constrain your results in a LINQ to Entity query.

Before we get started, we’ll use a simple example of a Contact entity that has one or more Address entities associated with it:

public class Contact
{
	public int ContactId { get; set; }
	public string FirstName { get; set; }
	public string LastName { get; set; }
	public DateTime Birthday { get; set; }
	public List<Address> Addresses { get; set; }
}

public class Address
{
	public int ContactId { ...

The Problem

I wanted to be able to add items to a IList<T> but not at the beginning or end of the list—in order based on a sorting function I specified.  I could have used List<T>’s Sort() method that takes an IComparer<T> as a parameter, but then I would have to define a new comparer class for every class I wanted to store in the list (not to mention every time I wanted to sort one those types off of a different property). 

The Solution

Instead of this potentially bulky solution, I created a generic extension method that will ...