linq (to nHibernate): 'like in' operator

前端 未结 3 1915
抹茶落季
抹茶落季 2021-01-22 16:46

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 emplo

相关标签:
3条回答
  • 2021-01-22 16:53

    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;
        }
    
    0 讨论(0)
  • 2021-01-22 16:59

    i used the following coding styles

    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

    var query = this.LINQ;
    query = query.Where(p => p.VendorNameAr.Contains(criteria.Keyword.Replace(" ", "%")) ||
                                                 p.VendorNameEN.Contains(criteria.Keyword.Replace(" ", "%")));
    return query.ToList();
    
    0 讨论(0)
  • 2021-01-22 17:11

    If you are not limited to the Linq provider but also open to the ICriteria API, I suggest using the following:

    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. With NH 3.0 you could use QueryOver, which would get rid of the magic strings.

    0 讨论(0)
提交回复
热议问题