Search code examples
.netcastle-activerecord

Large OR Expression in Castle.ActiveRecord


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?


Solution

  • 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.