[英]c# Nhibernate create query with dynamic parameters
using the folowing extention methods : 使用以下扩展方法:
public static void SetParameters(this IQuery query, List<object> Parameters) {
for (int i = 0; i < Parameters.Count(); i++) {
query.SetParameter(i, Parameters[i]);
}
}
public static IQuery SetQuery(this ISession session, string Query, object[] Parameters) {
return session.CreateSQLQuery(Query + string.Empty + "(" + ParseExt(Parameters) + ")");
}
private static string ParseExt(object[] Parameters) {
var str = new List<string>();
for (int i = 0; i < Parameters.Length; i++) {
str.Add(":" + i);
}
return string.Join(",", str);
}
i'm creating a query : 我正在创建一个查询:
public IEnumerable<T> Execute<T>(string Query, params dynamic[] Parameters) { using (var _session = _transactionManager.GetSession()) { var _cmd = _session.SetQuery(Query, Parameters); if (Parameters != null) { _cmd.SetParameters(Parameters.ToList()); } return _cmd.List<T>(); } }
throws the following error : 引发以下错误:
Remember that ordinal parameters are 1-based!
NOTE : i tried to change the index to start from 1 注意:我试图将索引更改为从1开始
The symbol for position-parameter is not :xxx
but ?
位置参数的符号不是
:xxx
而是?
. 。
This change will work 此更改将起作用
private static string ParseExt(object[] Parameters) {
var str = new List<string>();
for (int i = 0; i < Parameters.Length; i++) {
//str.Add(":" + i);
str.Add("?"); // + i);
}
return string.Join(",", str);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.