Querying with NHibernate

后端 未结 3 1601
迷失自我
迷失自我 2021-02-04 17:08

I am new to NHibernate and I am trying to learn how to query my data.

Below is the configuration xml. Only the recipe is shown.

I want to be able to query recipe

3条回答
  •  野的像风
    2021-02-04 17:35

    Here is the above criteria with dynamic keywords

    string searchQuery = "wine pasta";
    
    ICriteria query = Session.CreateCriteria(typeof(Recipe), "r")
                        .CreateCriteria("Ingredients", "i", JoinType.InnerJoin)
                        .SetFetchMode("Images", FetchMode.Join)
                        .SetFetchMode("Comments", FetchMode.Join)
                        .SetFetchMode("Ingredients", FetchMode.Join)
                        .SetResultTransformer(new DistinctRootEntityResultTransformer());
    
    var keywords = searchQuery.Split(' ');
    
    Disjunction keywordsCriteria = Restrictions.Disjunction();
    foreach (var keyword in keywords)
    {
        keywordsCriteria.Add(Restrictions.Like("i.IngredientName", string.Format("%{0}%", keyword)));
        keywordsCriteria.Add(Restrictions.Like("r.RecipeTitle", string.Format("%{0}%", keyword)));
    }
    
    query.Add(keywordsCriteria);
    
    List result = query.List();
    

提交回复
热议问题