I’m trying to get an object back from an NHibernate query.
My method is as follows:
public Site GetSiteByHost(string host)
{
var result = _session.CreateCriteria<Site>()
.Add(SqlExpression.Like<Site>(g => g.URLName, host));
return result;
}
the problem is, result is a type of HNibernate.ICriteria.
How can I get this to return a Site object?
If I was doing this with LINQ to SQL it’d be something like .FirstOrDefault() but that’s not available with NHibernate… or is it?!?!
You need to first execute the query (by calling
List<T>()on the criteria) before callingFirstOrDefault. Notice that this query might return multiple objects:And you could take the first one:
or directly: