tags:

views:

55

answers:

2

I have a method similar to the following, which I need to return all Groups that begin with the letter I'm passing in:

public IList<CompanyGroupInfo> GetGroupByQuery(string letter)
{
    IList<CompanyGroupInfo> result = null;

    result = _session
        .CreateCriteria<CompanyGroupInfo>()
        .Add(SqlExpression.Like<CompanyGroupInfo>(g => g.Name, letter))
        .List<CompanyGroupInfo>();

    return (result.Count > 0) ? result[0] : null;
}

I'm brand new to NHibernate, so I don't really know what to do here. In my mind, it'd be ideal if there was a SqlExpression.StartsWith method, but there isn't. Is it as simple as modifying the expression so that

        .Add(SqlExpression.Like<CompanyGroupInfo>(g => g.Name, letter))

becomes something like

        .Add(SqlExpression.Like<CompanyGroupInfo>(g => g.Name.StartsWith(letter)))

Can somebody please point me in the right direction? Thanks

+2  A: 

Just looking at the docs, I suspect you want:

SqlExpression.Like<CompanyGroupInfo>(g => g.Name, letter, MatchMode.Start)

but I haven't used NHibernate for ages...

Jon Skeet
+1  A: 

I think this

SqlExpression.Like<CompanyGroupInfo>(g => g.Name, letter + "%")
chrissie1