1
avatasia OP 好吧,single 如果结果多于2个就抛出异常
|
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 |
3
avatasia OP @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源码去 |
5
Ricepig 2012-04-25 00:12:23 +08:00
是要检查是不是多于1个吧。这个时候返回top 2不是很合理?
|
6
avatasia OP @thereisnoif 你要用sql server profiler去看
|
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 |
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
|