ExtensionMethod.NET Home of 880 C#, Visual Basic, F# and Javascript extension methods

FirstOrDefault

First or default extension method for NHibernate 3.0 and its new query API QueryOver.

Source

public static class QueryOverExtensions
{
    public static T FirstOrDefault<T, U>(this IQueryOver<T, U> queryOver)
        where T : class
        where U : class
    {
        return queryOver.Take(1).SingleOrDefault();
    }
}

Example

public class TeamEmployeeRepository : Repository<TeamEmployee>, ITeamEmployeeRepository
{
    public TeamEmployee GetFirstEmployee(string name)
    {
        return GetSession().QueryOver<TeamEmployee>()
            .Where(x => x.Name == name)
            .FirstOrDefault();
    }
}

Author: Mateusz Wajcowicz

Submitted on: 30 aug. 2013

Language: C#

Type: Nhibernate.QueryOver

Views: 6959