问题
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
回答1:
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...
回答2:
I think this
SqlExpression.Like<CompanyGroupInfo>(g => g.Name, letter + "%")
来源:https://stackoverflow.com/questions/3185023/how-to-get-results-that-begin-with-a-certain-letter