How to get results that begin with a certain letter?

☆樱花仙子☆ 提交于 2019-12-25 03:12:56

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!