NHibernate 3.0 - 当子查询没有用 EXISTS 引入时,只能在选择列表中指定一个表达式。”

发布于 2024-10-20 22:45:55 字数 1788 浏览 1 评论 0原文

我们正在尝试升级到 NHibernate 3.0,现在我遇到以下 Linq 查询问题。它返回“当子查询不带 EXISTS 引入时,选择列表中只能指定一个表达式。”错误。

这是控制器中的 linq 查询。

var list = (from item in ItemTasks.FindTabbedOrDefault(tab)
                    select new ItemSummary
                               {
                                   Id = item.Id,
                                   LastModifyDate = item.LastModifyDate,
                                   Tags = (from tag in item.Tags
                                           select new TagSummary
                                                      {
                                                          ItemsCount = tag.Items.Count,
                                                          Name = tag.Name
                                                      }).ToList(),
                                   Title = item.Title
                               });

以下是为此查询生成的 sql

select   TOP ( 1 /* @p0 */ ) item0_.Id             as col_0_0_,
                 item0_.LastModifyDate as col_1_0_,
                 (select (select cast(count(* ) as INT)
                          from   dbo.ItemsToTags items3_,
                                 dbo.Item item4_
                          where  tag2_.Id = items3_.Tag_id
                                 and items3_.Item_id = item4_.Id),
                         tag2_.Name
                  from   dbo.ItemsToTags tags1_,
                         dbo.Tag tag2_
                  where  item0_.Id = tags1_.Item_id
                         and tags1_.Tag_id = tag2_.Id) as col_2_0_,
                 item0_.Title          as col_3_0_ from     dbo.Item item0_ order by item0_.ItemPostDate desc

ps:如果我删除 linq 查询中的 Tags 属性,它工作正常。

查询的问题出在哪里?

提前致谢。

We are trying to upgrade to NHibernate 3.0 and now i am having problem with the following Linq query. It returns "Only one expression can be specified in the select list when the subquery is not introduced with EXISTS." error.

This is the linq query in the controller.

var list = (from item in ItemTasks.FindTabbedOrDefault(tab)
                    select new ItemSummary
                               {
                                   Id = item.Id,
                                   LastModifyDate = item.LastModifyDate,
                                   Tags = (from tag in item.Tags
                                           select new TagSummary
                                                      {
                                                          ItemsCount = tag.Items.Count,
                                                          Name = tag.Name
                                                      }).ToList(),
                                   Title = item.Title
                               });

and the following is the sql generated for this query

select   TOP ( 1 /* @p0 */ ) item0_.Id             as col_0_0_,
                 item0_.LastModifyDate as col_1_0_,
                 (select (select cast(count(* ) as INT)
                          from   dbo.ItemsToTags items3_,
                                 dbo.Item item4_
                          where  tag2_.Id = items3_.Tag_id
                                 and items3_.Item_id = item4_.Id),
                         tag2_.Name
                  from   dbo.ItemsToTags tags1_,
                         dbo.Tag tag2_
                  where  item0_.Id = tags1_.Item_id
                         and tags1_.Tag_id = tag2_.Id) as col_2_0_,
                 item0_.Title          as col_3_0_ from     dbo.Item item0_ order by item0_.ItemPostDate desc

ps:If i remove the Tags property in the linq query, it works fine.

Where is the problem in the query?

Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

柒夜笙歌凉 2024-10-27 22:45:55

我也遇到了同样的 Generic ADO Exception 错误,我认为这实际上是 SQL Server 的限制;
是否有可能以某种方式加载带有集合中投影的对象图?

如果我尝试这个:

var cats = q.Select(t => new cat()
                                          {
                                              NickName = t.NickName,
                                              Legs = t.Legs.Select(l => new Leg()
                                                                            {
                                                                                Color = l.Color,
                                                                                Size = l.Size
                                                                            }).ToList()
                                          }).ToList();

会出现同样的错误..

I've got the same Generic ADO Exception error, I think it's actually the limitation of SQL server;
Is it possible somehow load object graph with projections in collections?

If I try this one:

var cats = q.Select(t => new cat()
                                          {
                                              NickName = t.NickName,
                                              Legs = t.Legs.Select(l => new Leg()
                                                                            {
                                                                                Color = l.Color,
                                                                                Size = l.Size
                                                                            }).ToList()
                                          }).ToList();

That does the same error..

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