LINQ查询问题:返回多条记录

时间:2011-06-09 01:50:29

标签: sql-server linq

我有2张桌子。问题是无论我做什么,我都会得到多条记录。

  1. 子菜单
  2. 关系是1到1(每个子菜单都有一个页面)

    SubMenus示例数据:

    submenu_id  parentmenu_id   display_name    url_name
    ----------------------------------------------------
        1           1           Home            home    
        2           1           Contact         contact 
    

    Pages表数据:

    page_id     submenu_id      page_title          url_name
    ---------------------------------------------------------
       1            1           Home Page           home    
       2            1           Contact Page        null
    

    我想从那里SubMenu.UrlName == Home

    的JOIN中检索单个值
    (from s in SubMenus
     join p in Pages on s.Id equals p.SubMenuId
     where s.UrlName == "home"
     select new 
              {
                  s.Id, s.UrlName,
                  PageId = p.Id, p.Title, p.Html, 
                  p.MetaAuthor, p.MetaKeywords, p.MetaDescription 
              }).FirstOrDefault();
    

    但是,如果我检查SQL并运行它。我没有任何问题得到单一记录。下面是SQL生成的内容

    SELECT TOP (1) 
        [Extent1].[submenu_id] AS [submenu_id], 
        [Extent1].[url_name] AS [url_name], 
        [Extent2].[page_id] AS [page_id], 
        [Extent2].[page_title] AS [page_title], 
        [Extent2].[page_html] AS [page_html], 
        [Extent2].[meta_author] AS [meta_author], 
        [Extent2].[meta_keywords] AS [meta_keywords], 
        [Extent2].[meta_description] AS [meta_description]
    FROM  
        [dbo].[SubMenu] AS [Extent1]
    INNER JOIN 
        [dbo].[Pages] AS [Extent2] ON [Extent1].[submenu_id] = [Extent2].[submenu_id]
    WHERE 
        N'home' = [Extent1].[url_name]
    

    表格结构:

    CREATE TABLE [dbo].[ParentMenu]
    (
        [parentmenu_id] [int] IDENTITY(1,1) NOT NULL,
        [group_name] [nvarchar](50) NULL,
        [title] [nvarchar](100) NULL,
        [active] [bit] NOT NULL,
        [index_order] [int] NULL,
    
        CONSTRAINT [PK_ParentMenu] 
            PRIMARY KEY CLUSTERED ([parentmenu_id] ASC)
                        WITH (PAD_INDEX = OFF, STATISTICS NORECOMPUTE = OFF, 
                              IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, 
                              ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
    ) ON [PRIMARY]
    GO
    
    ALTER TABLE [dbo].[ParentMenu] 
        ADD CONSTRAINT [DF_ParentMenu_active] DEFAULT ((1)) FOR [active]
    GO
    
    ALTER TABLE [dbo].[ParentMenu] 
        ADD CONSTRAINT [DF_ParentMenu_index_order] DEFAULT ((0)) FOR [index_order]
    GO
    
    CREATE TABLE [dbo].[SubMenu]
    (
        [submenu_id] [int] IDENTITY(1,1) NOT NULL,
        [parentmenu_id] [int] NOT NULL,
        [display_name] [nvarchar](100) NULL,
        [url_name] [nvarchar](100) NULL,
        [index_order] [int] NULL,
        [active] [bit] NOT NULL,
    
        CONSTRAINT [PK_SubMenu] 
            PRIMARY KEY CLUSTERED ([submenu_id] ASC)
                        WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, 
                        IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, 
                        ALLOW_PAGE_LOCKS = ON) ON [PRIMARY],
    
        CONSTRAINT [IX_SubMenu_1] 
            UNIQUE NONCLUSTERED ([url_name] ASC)
                        WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, 
                        IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, 
                        ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
    ) ON [PRIMARY]
    GO
    
    ALTER TABLE [dbo].[SubMenu] WITH CHECK 
        ADD CONSTRAINT [FK_SubMenu_ParentMenu] 
            FOREIGN KEY([parentmenu_id]) REFERENCES [dbo].[ParentMenu] ([parentmenu_id])
                 ON DELETE CASCADE
    GO
    
    ALTER TABLE [dbo].[SubMenu] CHECK CONSTRAINT [FK_SubMenu_ParentMenu]
    GO
    
    ALTER TABLE [dbo].[SubMenu] 
        ADD CONSTRAINT [DF_SubMenu_index_order] DEFAULT ((0)) FOR [index_order]
    GO
    
    ALTER TABLE [dbo].[SubMenu] 
        ADD CONSTRAINT [DF_SubMenu_active]  DEFAULT ((1)) FOR [active]
    GO
    
    CREATE TABLE [dbo].[Pages]
    (
        [page_id] [int] IDENTITY(1,1) NOT NULL,
        [submenu_id] [int] NOT NULL,
        [page_title] [nvarchar](200) NULL,
        [url_name] [nvarchar](100) NULL,
        [page_html] [nvarchar](max) NULL,
        [meta_author] [nvarchar](300) NULL,
        [meta_keywords] [nvarchar](max) NULL,
        [meta_description] [nvarchar](max) NULL,
        [active] [bit] NOT NULL,
        [creation_date] [date] NULL,
    
        CONSTRAINT [PK_Pages] 
            PRIMARY KEY CLUSTERED ([page_id] ASC)
                        WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE = OFF, 
                              IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, 
                              ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
    ) ON [PRIMARY]
    GO
    
    ALTER TABLE [dbo].[Pages] WITH CHECK 
        ADD CONSTRAINT [FK_Pages_SubMenu] 
            FOREIGN KEY([submenu_id]) REFERENCES [dbo].[SubMenu] ([submenu_id])
                 ON DELETE CASCADE
    GO
    
    ALTER TABLE [dbo].[Pages] CHECK CONSTRAINT [FK_Pages_SubMenu]
    GO
    
    ALTER TABLE [dbo].[Pages] 
        ADD CONSTRAINT [DF_Pages_active] DEFAULT ((1)) FOR [active]
    GO
    

1 个答案:

答案 0 :(得分:0)

您说的是Problem is I always get multiple records no matter what I do.

您显示的查询在第一行中有top 1,它将始终只返回一行。

它出了什么问题?

从我看到你的SQL查询与你的LINQ查询所做的匹配。

在这种情况下,LINQ查询不可能返回比SQL返回更多的行。

相关问题