tags:

views:

37

answers:

2

I see that Session.Find(string query, object[] values, IType[] types) is obsolete, and the suggestion is to use CreateQuery().SetParameterList().List() instead.

So if I already have code that looks like this:

var query = "from TABLE t where t.COL1 = ? and t.COL2 = ?";
var vals = new[] { qc1, qc2 };
var types = new[] { ScalarType.String, ScalarType.String };

Session.Find(query, vals, types);

What would I pass to SetParameterList's name argument?

+1  A: 

What you're looking for is probably something like this:

session.CreateQuery("from Entity t where t.COL1 = :col1 and t.COL2 = :col2")
    .SetString("col1", qc1)
    .SetString("col1", qc2)
    .List<Entity>();

.SetParameterList(...) takes an ICollection as argument, and can be used e.g. with the in clause:

session.CreateQuery("from Entity t where t.COL1 in (:cols)")
    .SetParameterList("cols", new [] { "someValue", "anotherValue", "etc"})
    .List<Entity>();
mookid8000
SetString is useful if I know the argument is a string, but I have thousands of these queries, most of which are generated, some via reflection, along with the value and type arrays. The queries don't name the parameter placeholders -- they all simply specify '?'.
Wayne
A: 

I think I have to do this:

var q = Session.CreateQuery(query);
for (int i = 0; i < vals.Length; i++)
{
  q.SetParameter(i, vals[i], types[i]);
}
Wayne
Use .SetParameterList("cols", vals).
Rafael Belliard
Doesn't specify the types
Wayne