如何在 MultiQuery 中设置查询参数? “指定的参数代码被用于多个查询。”

发布于 2024-12-11 10:57:25 字数 720 浏览 0 评论 0原文

我正在尝试为 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

ゃ懵逼小萝莉 2024-12-18 10:57:25

也许不太干净,但您是否想过为参数使用唯一的 id?

int i = 0;
foreach (string name in names)
{
    string paramname = "name" + (++i).ToString();
    var query = SessionHolder.Current
        .CreateQuery("select c.Name, c.Surname " +
                     "from Person as p " +
                     "where p.Name = :" + paramname + " or " +
                           "p.Name like ':" + paramname + "/%'")
        .SetParameter(paramname, name);
    multiQuery = multiQuery.Add(query);
}

Maybe is not too clean but did u think to use an unique id for the parameter?

int i = 0;
foreach (string name in names)
{
    string paramname = "name" + (++i).ToString();
    var query = SessionHolder.Current
        .CreateQuery("select c.Name, c.Surname " +
                     "from Person as p " +
                     "where p.Name = :" + paramname + " or " +
                           "p.Name like ':" + paramname + "/%'")
        .SetParameter(paramname, name);
    multiQuery = multiQuery.Add(query);
}
寄居人 2024-12-18 10:57:25

您需要考虑多重查询实际上在做什么。

在幕后,您将最终对数据库进行一次查询,如下所示:

select p.Name, p.Surname from Person as p where p.Name = :name or p.Name like ':name/%'
select p.Name, p.Surname from Person as p where p.Name = :name or p.Name like ':name/%'
select p.Name, p.Surname from Person as p where p.Name = :name or p.Name like ':name/%'
select p.Name, p.Surname from Person as p where p.Name = :name or p.Name like ':name/%'
select p.Name, p.Surname from Person as p where p.Name = :name or p.Name like ':name/%'
...

您可能不希望查询的每个部分都使用相同的条件,因为这没有多大意义。如果您想要这样做,您应该使用唯一的名称。

但是,查看您的查询,您可能希望将其重写为具有多个条件的单个查询。

也许有一个标准更像是:

p.Name in (:names) or (p.Name like ':name1/%' or p.Name like ':name2/%' ...)

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:

select p.Name, p.Surname from Person as p where p.Name = :name or p.Name like ':name/%'
select p.Name, p.Surname from Person as p where p.Name = :name or p.Name like ':name/%'
select p.Name, p.Surname from Person as p where p.Name = :name or p.Name like ':name/%'
select p.Name, p.Surname from Person as p where p.Name = :name or p.Name like ':name/%'
select p.Name, p.Surname from Person as p where p.Name = :name or p.Name like ':name/%'
...

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:

p.Name in (:names) or (p.Name like ':name1/%' or p.Name like ':name2/%' ...)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文