gpt4 book ai didi

nhibernate - 查询开始于

转载 作者:行者123 更新时间:2023-12-04 17:31:18 25 4
gpt4 key购买 nike

我必须尝试根据搜索找到结果,应该是startswith,但我找不到QueryOver有startwith,还有其他方法吗?

public IEnumerable<Company> Find(string keyword)
{
var sesion = SessionController.CurrentSession;
return sesion.QueryOver<Company>()
.Where(
Restrictions.On<Company>(x => x.CompanyName).IsLike("%" + keyword + "%") ||
Restrictions.On<Company>(x => x.Id).IsLike("%" + keyword + "%")
)
.List<Company>();
}

最佳答案

方式,支持 QueryOver基础设施将使用显式 MatchMode :

sesion
.QueryOver<Company>()
.Where
(
Restrictions.On<Company>(x => x.CompanyName).IsLike(keyword, MatchMode.Start) ||
Restrictions.On<Company>(x => x.Id ).IsLike(keyword, MatchMode.Start)
)
.List<Company>();

但是使用很少的代码( Restrictions.On 样式中的自定义扩展)我们甚至可以实现以下语法:
...
.Where
(
Restrict.On<Contact>(x => x.CompanyName).StartsWith(keyword) ||
Restrict.On<Contact>(x => x.Id ).StartsWith(keyword)
)

以及 Restrict的定义:
public static class Restrict
{
public static StringRestrictionBuilder<T> On<T>(Expression<Func<T, object>> expr)
{
return new StringRestrictionBuilder<T>(expr);
}
public class StringRestrictionBuilder<T>
{
readonly Expression<Func<T, object>> _expression;
public StringRestrictionBuilder(Expression<Func<T, object>> expression)
{
_expression = expression;
}
public AbstractCriterion StartsWith(string value)
{
return Restrictions.On(_expression).IsLike(value, MatchMode.Start);
}
public AbstractCriterion Contains(string value)
{
return Restrictions.On(_expression).IsLike(value, MatchMode.Anywhere);
}
public AbstractCriterion EndsWith(string value)
{
return Restrictions.On(_expression).IsLike(value, MatchMode.End);
}
}
}

关于nhibernate - 查询开始于,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24485619/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com