views:

299

answers:

2

Hello. I am triyng to a do psuedo-fuzzy logic query in active record.

How do I express:

field1 like 'A' or field2 like 'A' or field3 like 'A' or field1 like 'B' or field2 like 'B' or field3 like 'B'

using the ICriteria objects...where the 'likes' need to be InsensitiveLikeExpressions?

A: 

Its not ICriteria objects, but in HQL the answer is this:

string hql = "FROM ARAddressableLocation as al WHERE (LOWER(al.SubNumber) = '{0}' OR LOWER(al.Number) = '{0}' OR...";
ARAddressableLocation[] obj = (ARAddressableLocation[])Execute(
    delegate(ISession session, object instance) {
      IQuery query = session.CreateQuery(hql);
      IList results = query.List();

      ARAddressableLocation[] list = new ARAddressableLocation[results.Count];
      results.CopyTo(list, 0);

      return list;
    }, null);

My fields are all always strings, so I elected to put them in myself instead of using NHibernate's parameters.

(votes up for the answer would be appreciated)

Ash
Does "WHERE AND >" work? Plus, not using proper parameters can be dangerous: http://ayende.com/Blog/archive/2006/09/30/ORMAndSQLInjections.aspx
Mauricio Scheffer
Opps that was a typo sorry. I will fix it.
Ash
+2  A: 

How about this:

    public T[] FuzzyFind<T>(string[] fields, string[] values) where T: class {
        var dis = new Disjunction();
        foreach (var f in fields)
            foreach (var v in values)
                dis.Add(new InsensitiveLikeExpression(f, v, MatchMode.Anywhere));
        return ActiveRecordMediator<T>.FindAll(dis);
    }

Then you call it like this:

var locations = FuzzyFind<ARAddressableLocation>(new[] { "field1", "field2", "field3 }, new[] { "a", "b" });

which produces all combinations of (field, value)

Adjust MatchMode as needed.

Mauricio Scheffer