views:

2104

answers:

2

Hi,

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!

A: 

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

Alex Reitbort
Hi Alex, this is similar to Expression.In but it's still the opposite of what I require. I want to know if Value exists in PropertyName. Not if PropertyName exists in Value
Unless... 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?
+1  A: 

If you want to know if 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.

jedierikb
how would you go about doing this using IQuery?