我有一个搜索方法如下。我希望在不同表的某些列上拆分和搜索用户搜索文本。
string[] searchedTexts = null;
if(searchText!=null)
searchedTexts = searchText.Split(new[] { ',', ' ' }, StringSplitOptions.RemoveEmptyEntries);
WebsitePage page = null;
Account account = null;
AccountLogin login = null;
Author author = null;
var query = Session.QueryOver<WebsitePage>(() => page).Where(() => page.Status == WebsitePageStatus.Online).Left
.JoinQueryOver<Author>(x => x.Blogger, () => author).Left
.JoinQueryOver<Account>(x => x.AccountInfo, () => account).Left
.JoinQueryOver<AccountLogin>(x => x.Logins, () => login);
if (searchedTexts != null)
{
var disconjaction = Expression.Disjunction();
foreach (var text in searchedTexts)
{
disconjaction.Add(Expression.Like(Projections.Property(() => page.Title), text, MatchMode.Anywhere));
disconjaction.Add(Expression.Like(Projections.Property(() => page.Body), text, MatchMode.Anywhere));
disconjaction.Add(Expression.Like(Projections.Property(() => page.TagCommaSeperated), text, MatchMode.Anywhere));
disconjaction.Add(Expression.Like(Projections.Property(() => account.FirstName), text, MatchMode.Anywhere));
disconjaction.Add(Expression.Like(Projections.Property(() => account.LastName), text, MatchMode.Anywhere));
disconjaction.Add(Expression.Like(Projections.Property<AccountLogin>(x => x.UserName), text, MatchMode.Anywhere));
}
query.And(disconjaction);
}
return query.List();
正如您在 Expression.Like() 方法中看到的那样,我使用了相同的参数。执行后,nhibernate 为每个“Like”表达式添加不同的参数,例如:
@p1=N'%test%',@p2=N'%test%',@p3=N'%test%',@p4=N'%search%',@p5=N'%search%',@p6=N'%search%'
在 foreach 的每个循环中,我想使用一个带有所有 6 个“Like”表达式的 sqlparameter。可能吗 ?