English 中文(简体)
下面的林克为什么会引发一种子 the?
原标题:Why did the following linq to sql query generate a subquery?

我提出了以下问题:

var list = from book in books
          where book.price > 50
          select book;

list = list.Take(50);

我期望上述内容能够产生这样的结果:

SELECT top 50 id, title, price, author
FROM Books
WHERE price > 50

但它产生了:

SELECT
[Limit1].[C1] as [C1]
[Limit1].[id] as [Id], 
[Limit1].[title] as [title], 
[Limit1].[price] as [price], 
[Limit1].[author]
FROM (SELECT TOP (50) 
             [Extent1].[id] as as [Id], 
             [Extent1].[title] as [title], 
             [Extent1].[price] as [price], 
             [Extent1].[author] as [author]
      FROM Books as [Extent1]
      WHERE [Extent1].[price] > 50
     ) AS [Limit1]

为什么上述林木矿会产生一个子阶,C1从哪里产生?

最佳回答

你们仍然可以这样作:

var c = (from co in db.countries
                    where co.regionID == 5
                    select co).Take(50);

这将导致:

Table(country).Where(co => (co.regionID = Convert(5))).Take(50)

相当于:

SELECT TOP (50) [t0].[countryID], [t0].[regionID], [t0].[countryName], [t0].[code]
FROM [dbo].[countries] AS [t0]
WHERE [t0].[regionID] = 5

EDIT: 评论,不一定是因为有单独的请购单,你仍然可以这样使用:

var c = (from co in db.countries
                     where co.regionID == 5
                     select co);
            var l = c.Take(50).ToList();

结果与以前相同。

SELECT TOP (50) [t0].[countryID], [t0].[regionID], [t0].[countryName], [t0].[code]
FROM [dbo].[countries] AS [t0]
WHERE [t0].[regionID] = @p0

页: 1 IQueryable = IQueryable.Take(50)是这里的trick部分。

问题回答

<>Disapper: 此前从未使用过准则编号。

我的猜测是支持吗? 我猜测你有某种类型的<代码>(50,50)方法,这些方法有50个记录,从记录50开始。 看一下KQ引起的询问,你可能会发现,它使用类似的次问处理结构,使它能够在大约50个牢房中返回。

不管怎样,在拟定执行计划期间,nes子问并没有增加任何绩效管理费用,因为它自动地选择取消。

为了投影目的,当你从多个表格中选择单一匿名物体,然后利用外部查询收集结果时,就更有意义。

对这种情况的发生:

from book in books
where price > 50
select new 
{
  Title = book.title,
  Chapters = from chapter in book.Chapters
             select chapter.Title
}

第一种询问是否将总行数退回,而第二点则根据对“ake())”方法的号召提取分行?

  1. I agree with @Justin Swartsel. There was no error involved, so this is largely an academic matter.
  2. Linq-to-SQL endeavors to generate SQL that runs efficiently (which it did in your case).
    1. But it does not make any effort to generate conventional SQL that a human would likely create.
  3. The Linq-to-SQL implementers likely used the builder pattern to generate the SQL.
    1. If so, it would be easier to append a substring (or a subquery in this case) than it would be to backtrack and insert a TOP x fragment into the SELECT clause.




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签