linq (to nHibernate): 'like in' operator

前端 未结 3 1917
抹茶落季
抹茶落季 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 17:11

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

    List fullnames = new List() { "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 list = query.List();
    

    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.

提交回复
热议问题