V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX 提问指南
avatasia
V2EX  ›  问与答

C#, linq to sql, 为嘛single生成的sql 是top (2)而不是top (1)

  •  
  •   avatasia · 2012-04-20 19:46:38 +08:00 · 4887 次点击
    这是一个创建于 4388 天前的主题,其中的信息可能已经有所发展或是发生改变。
    SELECT TOP (2)
    [Extent1].[Id] AS [Id],
    [Extent1].[Name] AS [Name],
    [Extent1].[Url] AS [Url],
    [Extent1].[ParentId] AS [ParentId]
    FROM [dbo].[MenuItem] AS [Extent1]
    WHERE [Extent1].[ParentId] IS NULL
    10 条回复    1970-01-01 08:00:00 +08:00
    avatasia
        1
    avatasia  
    OP
       2012-04-20 19:48:32 +08:00
    好吧,single 如果结果多于2个就抛出异常
    thereisnoif
        2
    thereisnoif  
       2012-04-24 18:57:15 +08:00
    生成的sql应该是:
    SELECT [t0].[id] as [id]
    FROM [dbo].[table] as [t0]
    WHERE [t0].[id] IS NULl
    这样的,没有TOP语句;

    Single扩展方法Returns the only element of a sequence that satisfies a specified condition, and throws an exception if more than one such element exists.

    http://msdn.microsoft.com/en-us/library/bb535118.aspx
    avatasia
        3
    avatasia  
    OP
       2012-04-24 19:35:16 +08:00
    @thereisnoif 你错了
    thereisnoif
        4
    thereisnoif  
       2012-04-24 23:34:15 +08:00
    @avatasia 我用linqpad实验的结果显示,Single生成的sql是没有Top的;First会产生Top语句;不过http://stackoverflow.com/questions/8829314/why-does-single-linq-extension-method-create-a-query-select-top2 确实是如你所说的情况。
    也许是Linqpad显示的sql有问题?
    明天去看看Single源码去
    Ricepig
        5
    Ricepig  
       2012-04-25 00:12:23 +08:00
    是要检查是不是多于1个吧。这个时候返回top 2不是很合理?
    avatasia
        6
    avatasia  
    OP
       2012-04-25 09:28:22 +08:00
    @thereisnoif 你要用sql server profiler去看
    avatasia
        7
    avatasia  
    OP
       2012-04-25 09:29:09 +08:00
    @Ricepig single 是取唯一的一个,first是取多个中的第一个,所以single要做多于1的判断
    thereisnoif
        8
    thereisnoif  
       2012-04-25 10:49:56 +08:00
    @avatasia 很奇怪啊,我确实用sql profiler看了,是没有top的。。。
    难道跟Linq版本有关,我是4.0;
    C#查询是:
    var ret = App_Case_Alls.Single(c => c.Case_type == 1);
    Console.Write(ret);

    sql profilerie截到的sql是:

    exec sp_executesql N'SELECT [t0].[case_id] AS [Case_id], [t0].[case_type] AS [Case_type], [t0].[req_no] AS [Req_no]
    FROM [app_Case_All] AS [t0]
    WHERE [t0].[case_type] = @p0',N'@p0 int',@p0=1
    Ricepig
        9
    Ricepig  
       2012-04-25 15:21:34 +08:00
    @avatasia 我说的就是这个意思。。。。
    thereisnoif
        10
    thereisnoif  
       2012-04-25 16:18:09 +08:00   ❤️ 1
    @avatasia 终于弄清楚了,用Entity Framework的Linq查询Single,确实生成的sql是TOP(2); 但用Linq-to-Sql的话,Single是不会有产生TOP语句的;我的实验都是直接用Linq-to-sql的,所以才产生困扰。see http://stackoverflow.com/questions/10309100/does-linq2sql-generated-sql-contains-top-or-not-when-single-is-used
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5651 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 359ms · UTC 06:31 · PVG 14:31 · LAX 23:31 · JFK 02:31
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.