[英]linq (to nHibernate): 'like in' operator
Hi 你好
Given a list of strings I want to retrieve all items whose names contain one of the given strings. 给定一个字符串列表,我想检索名称包含给定字符串之一的所有项。
for example- given {"foo", "kuku"} I want to retrieve the employees "Corfoo", "kuku maluku" and "kukufoo". 例如,给定{“ foo”,“ kuku”},我想检索员工“ Corfoo”,“ kuku maluku”和“ kukufoo”。
I've tried the following, but got a null-reference exception(?) 我尝试了以下操作,但是有一个空引用异常(?)
query.Where(u => values.Any(v=> u.FullName.Contains(v)) );
The following produced a 'Lambda expression not in scope' exception. 以下产生了“ Lambda表达式不在范围内”异常。
query.Where(u => (values.Count(v => u.FullName.Contains(v)) > 0) );
any idea how this can be done? 任何想法如何做到这一点?
I was thinking along the lines of iterating over the values collection and adding a new condition for each element. 我一直在考虑遍历值集合并为每个元素添加新条件的思路。
problem is- the .Where() function is a conjunction (AND) and I need disjunction (OR)... 问题是-.Where()函数是一个连词(AND),我需要析取(OR)...
(I'm using nH 2.1.2 with Linq provider; hadn't tried this on nH3.0 yet...) (我在Linq provider上使用nH 2.1.2;尚未在nH3.0上尝试过此方法...)
If you are not limited to the Linq provider but also open to the ICriteria API, I suggest using the following: 如果您不仅限于Linq提供程序,而且还可以使用ICriteria API,则建议使用以下方法:
List<string> fullnames = new List<string>() { "foo", "kuku" };
// prepare Query
var query = session.CreateCriteria(typeof(Employee));
// dynamically add Like-conditions combined with OR
Disjunction namesCriteria = Restrictions.Disjunction();
foreach (var name in fullnames)
{
namesCriteria.Add(Restrictions.Like("FullName", name, MatchMode.Anywhere));
}
// add complete Disjunction to prepared query
query.Add(namesCriteria);
IList<Employee> list = query.List<Employee>();
I think trying that in NHibernate.Linq might be harder if not impossible. 我认为在NHibernate.Linq中尝试这样做可能会更困难,即使不是不可能。 With NH 3.0 you could use QueryOver, which would get rid of the magic strings.
在NH 3.0中,您可以使用QueryOver,它可以摆脱魔术字符串。
I Used the following code hope it helps; 我使用以下代码希望对您有所帮助;
public IList<AutoCompleteDto> GetCitiesLike(string text) { AutoCompleteDto autoCompleteDto = null; var cityList = UnitOfWork.CurrentSession.QueryOver<City>() .Where(x => x.CityName.IsLike(text, MatchMode.Start)) .SelectList(u => u .Select(x => x.Id).WithAlias(() => autoCompleteDto.Id) .Select(x => x.CityName).WithAlias(() => autoCompleteDto.Name) .Select(x => x.CityName).WithAlias(() => autoCompleteDto.Value)) .TransformUsing(Transformers.AliasToBean<AutoCompleteDto>()) .List<AutoCompleteDto>(); return cityList; }
i used the following coding styles 我使用了以下编码样式
QueryOver QueryOver
IQueryOver<Patient> rowCount = Session.QueryOver<Patient>().ToRowCountQuery();
IQueryOver<Patient> result = this.Session.QueryOver<Patient>()
.Where(p => (p.FullNameEn.IsLike("%" + criteria.Keyword.Replace(" ", "%") + "%"))
|| (p.FullNameAr.IsLike("%" + criteria.Keyword.Replace(" ", "%") + "%"))
|| (p.IdentityNO == criteria.Keyword)
|| (p.MobileNO == criteria.Keyword)
|| (p.PatientID == patientIDKeyword)
)
.OrderBy(p => p.FullNameEn).Asc
.Take(criteria.PageSize)
.Skip((criteria.Page - 1) * criteria.PageSize);
totalCount = result.ToRowCountQuery().FutureValue<int>().Value;
transaction.Commit();
return result.Future<Patient>().ToList();
LINQ LINQ
var query = this.LINQ;
query = query.Where(p => p.VendorNameAr.Contains(criteria.Keyword.Replace(" ", "%")) ||
p.VendorNameEN.Contains(criteria.Keyword.Replace(" ", "%")));
return query.ToList();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.