Hi all,
I have a weird problem involving hql and single quotes. I have the following code:
string hql = "from Recipe where ";
List<string> queryParts = new List<string>();
Code:
if(!string.IsNullOrEmpty(recipe.Title))
queryParts.Add(string.Format("Title LIKE '%{0}%'", recipe.Title.Replace("'", "''")));
if(!string.IsNullOrEmpty(recipe.RecipeId))
queryParts.Add(string.Format("RecipeId LIKE '%{0}%'", recipe.RecipeId));
for (int i = 0; i < queryParts.Count; i++)
{
hql += queryParts[i];
if (i == (queryParts.Count - 1))
break;
hql += " or ";
}
hql += " order by UpdateTimestamp";
ISession session = _sessionManager.OpenSession();
IQuery q = session.CreateQuery(hql);
return q.List<Recipe>();
which renders the following hql:
Code:
from Recipe where Title LIKE '%Warm Butternut Squash And Goat''s Cheese Salad%' order by UpdateTimestamp
The thing to notice is that I am replacing single quotes with double single quotes, e.g. Goat's becomes Goat's.
The problem is I am not getting any results back. The more curious thing is that it generates the following sql which actually returns a result set of 1:
Code:
select recipe0_.Uid as Uid23_, recipe0_.updatetimestamp as updateti2_23_, recipe0_.NumberOfImages as NumberOf3_23_, recipe0_.Title as Title23_, recipe0_.RecipeId as RecipeId23_, recipe0_.Summary as Summary23_, recipe0_.PublishedDate as Publishe7_23_, recipe0_.Visits as Visits23_, recipe0_.TempUid as TempUid23_, recipe0_.SMSIngredients as SMSIngr10_23_, recipe0_.GoLiveDate as GoLiveDate23_, recipe0_.RatingsTotal as Ratings12_23_, recipe0_.UsersTotal as UsersTotal23_ from Recipes recipe0_ where (Title LIKE '%Warm Butternut Squash And Goat''s Cheese Salad%') order by UpdateTimestamp
I am very confused. How come the List<> method returns a count of 0 but the generated sql returns a count of 1?
Thanks
Paul