In your method, you want to build your XML string. I didn't create an XML doc and add it that way. This is simple, so I used a string builder to create the xml by hand.
StringBuilder sb = new StringBuilder(); sb.Append("<StoreIds>"); foreach (int storeId in storeIds) { sb.AppendFormat("<Id>{0}</Id>", storeId); } sb.Append("</StoreIds>");
Then I passed the xml string into my GetNamedQuery call.
var query = Session.GetNamedQuery("MyAwesomeNamedQuery") .SetParameter("userId", userId) .SetParameter("condition", whereClause) .SetParameter("storeIds", sb.ToString(), NHibernateUtil.StringClob); result = query.List<MyObject>().AsQueryable();
NOTE: The key to this is defining the third parameter of the SetParameter method as NHibernateUtil.StringClob. If you don't, nHibernate automatically truncates your string to 4,000 characters.
The SQL statement was changed from using
INSERT INTO @storeIds2
SELECT StoreId FROM Client.Store
WHERE StoreId IN (:storeIds)
To
INSERT INTO @storeIds2 SELECT T.x.value('.', 'int') ID FROM @storeIds.nodes('/StoreIds/Id') T(x)
And that's all the SQL it takes to extract the ID's from the xml string we passed into a sql @table variable!
When I tested this code, I tested it with a db that held 11,500+ stores. All stores were passed perfectly.
No comments:
Post a Comment