如何在 MultiQuery 中设置查询参数? “指定的参数代码被用于多个查询。”
我正在尝试为 MultiQuery 中的查询设置参数:
var multiQuery = SessionHolder.Current.CreateMultiQuery();
foreach (string name in names)
{
var query = SessionHolder.Current
.CreateQuery("select c.Name, c.Surname " +
"from Person as p " +
"where p.Name = :name or " +
"p.Name like ':name/%'")
.SetParameter("name", name);
multiQuery = multiQuery.Add(query);
}
var multiQueryResult = multiQuery.List();
并收到此异常:
QueryException 未由用户代码处理
命名参数代码用于多个查询中。为参数提供唯一的名称,或者使用多查询 SetParameter() 方法来设置命名参数
如何使用多查询 SetParameter() 方法来设置命名参数,以便示例按预期工作?
感谢您的回复!
I am trying to set a parameter for a Query inside a MultiQuery:
var multiQuery = SessionHolder.Current.CreateMultiQuery();
foreach (string name in names)
{
var query = SessionHolder.Current
.CreateQuery("select c.Name, c.Surname " +
"from Person as p " +
"where p.Name = :name or " +
"p.Name like ':name/%'")
.SetParameter("name", name);
multiQuery = multiQuery.Add(query);
}
var multiQueryResult = multiQuery.List();
and getting this exception:
QueryException was unhandled by user code
The named parameter code was used in more than one query. Either give unique names to your parameters, or use the multi query SetParameter() methods to set the named parameter
How can I use the multi query SetParameter() methods to set the named parameter so the example works as intended?
Thanks for replies!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
也许不太干净,但您是否想过为参数使用唯一的 id?
Maybe is not too clean but did u think to use an unique id for the parameter?
您需要考虑多重查询实际上在做什么。
在幕后,您将最终对数据库进行一次查询,如下所示:
您可能不希望查询的每个部分都使用相同的条件,因为这没有多大意义。如果您想要这样做,您应该使用唯一的名称。
但是,查看您的查询,您可能希望将其重写为具有多个条件的单个查询。
也许有一个标准更像是:
You need to consider what multiquery is actually doing.
Behind the scenes you're going to end up with a single query to the database that looks like this:
You probably don't want the same criteria in each part of the query as that doesn't make much sense. You should be using unique names if this is what you want to do.
However, looking at your query you probably want to rewrite it into a single query with multiple conditions anyway.
Perhaps something with a criteria more like: