HQL “Contains” statement howto?

2019-05-11 12:53发布

问题:

I have an entity that has a string property called Tags. I would like to query this entity based upon if a certain string is located in Tags property.

So for example, I would have a function IList GetEntityByTag(string tag), this would return all Entity's that have the value of tag in their 'Tags' property.

I tried going through the ICriteria approach... Expression.In(PropertyName, Value) but this is the exact opposite. I need something like Expression.In(Value, PropertyName).

Perhaps IQuery would be a better strategy but I have not been able to find any type of HQL statment for Property CONTAINS 'abc'.

Any help or point of direction would be great thanks!

回答1:

Do you mean Expression.Like(PropertyName, Value)?



回答2:

If you want to know if a tag is a substring in your Tags property, you might want to consider these tips:

  • You might want to convert both the string you are searching in and searching for to lowercase first. Expression.ilike does this for you. Score.
  • To find out if your search term is anywhere in the field, you can set the MatchMode parameter in the ilike function to MatchMode.ANYWHERE.

If, as you commented earlier,

let's say my property, 'Tags' = a;b;c;d;e. I want to know if 'a' exists in tags. Will Expression.Like("Tags", "a") return true?

If 'a;b;c;d;e' is a string, Expression.ilike( "Tags", "a", MatchMode.ANYWHERE ) will return true.