lesscode
lesscode

Reputation: 6361

NHibernate: Now that Session.Find() is obsolete, how do I replace this?

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?

Upvotes: 1

Views: 1639

Answers (2)

lesscode
lesscode

Reputation: 6361

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]);
}

Upvotes: 0

mookid8000
mookid8000

Reputation: 18628

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>();

Upvotes: 2

Related Questions