代码之家  ›  专栏  ›  技术社区  ›  Erik Öjebo

NHibernate:HQL等价于条件表达式.In()?

  •  1
  • Erik Öjebo  · 技术社区  · 16 年前

    var idArray = new int[] { 1, 2, 3, 4, 5 };
    
    Session.CreateCriteria(typeof(Foo))
        .Add(Expression.In("Id", idArray)
        .List<Foo>();
    

    我知道HQL中有一个“in”关键字,但据我所知,该关键字用于子查询,而不是类似“…whereid in(1,2,3,4,5)”之类的东西。如果不是这样,我将乐意接受更正。

    谢谢/埃里克

    2 回复  |  直到 16 年前
        1
  •  5
  •   Darin Dimitrov    16 年前

    试试这个:

    var idArray = new int[] { 1, 2, 3, 4, 5 };
    var foos = Session
        .CreateQuery("from Foo f where f.Id in (:ids)")
        .SetParameterList("ids", idArray)
        .List<Foo>();
    
        2
  •  1
  •   Constantin    14 年前

    这也行

    ICriteria sc = session.CreateCriteria(typeof(Foo));
    sc.Add(Restrictions.In("id",new[] { 1, 2 }));
    siteList = sc.List();
    session.Close();