微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

c# – 为什么以下linq到sql查询生成一个子查询?

我做了以下查询
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]

为什么上述linq查询生成一个查询,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

编辑:评论,它不一定是因为单独的Take(),你仍然可以这样使用它:

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].[code]
FROM [dbo].[countries] AS [t0]
WHERE [t0].[regionID] = @p0

你写的IQueryable = IQueryable.Take(50)的事实在这里很棘手.

原文地址:https://www.jb51.cc/csharp/95851.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐