LINQ - 最小化返回的记录 - 编写这些表达式的正确方法

发布于 2024-08-17 19:48:21 字数 557 浏览 9 评论 0原文

Employee 是一个示例实体类型。

var r1 = (from c in _ctx select c).Skip(5).Take(5);  

// my intent is to pull the first record from the query  
var r2 = (from c in _ctx select c).FirstOrDefault<Employee>();   

// my intent is to pull the last record from the query.
// any good way to ask for the result back in the reverse   
// order of the natural sort without specifing a field/property name?  
var r3 = (from c in _ctx select c).LastOrDefault<Employee>();  

这些是否会拉回整个记录(对象)然后进行过滤? 编写这些以使整行都是 LINQ 表达式的最佳方法是什么?

Employee is a sample entity type.

var r1 = (from c in _ctx select c).Skip(5).Take(5);  

// my intent is to pull the first record from the query  
var r2 = (from c in _ctx select c).FirstOrDefault<Employee>();   

// my intent is to pull the last record from the query.
// any good way to ask for the result back in the reverse   
// order of the natural sort without specifing a field/property name?  
var r3 = (from c in _ctx select c).LastOrDefault<Employee>();  

Do these pull back the entire records (objects) and then filter?
What is the best way to write these so that the whole line is a LINQ expression?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

夜唯美灬不弃 2024-08-24 19:48:21

我相信(假设 _ctx 是一个 DataContext)生成的 SQL 会比较高效。

我可以建议您在运行这些代码时运行 SQL Server Profiler 吗?

我认为对 r2 的查询将是一个简单的 SELECT TOP (1) 语句。

r1 有机会高效地使用:

SELECT TOP(5) [fields]
FROM (
       SELECT TOP (10) [fields]
       FROM [table]
       ORDER BY [identitycol] DESC
     )

r3 看起来它可能只是选择全部并获取最后一条记录。

尝试 Profiler 并查看:)

I believe that (assuming _ctx is a DataContext) the SQL generated will be mildly efficient.

Can I suggest that you run SQL Server Profiler while you run these bits of code?

I think the query for r2 will be a simple SELECT TOP (1) statement.

r1 has a chance of being efficient with:

SELECT TOP(5) [fields]
FROM (
       SELECT TOP (10) [fields]
       FROM [table]
       ORDER BY [identitycol] DESC
     )

r3 looks like it may just select all and take the last record.

Try Profiler and see :)

§对你不离不弃 2024-08-24 19:48:21

如果您使用 IQueryable,则意味着您创建将在远程源中运行的表达式树

请参阅 IQueryable和 IQueryable之间有什么区别和 IEnumerable

好书工具 c# 3.0 in a NutShell、LinqPad、LINQ in Action

If you use IQueryable that means that you create a expression tree that will be run in the remote source

See What is the difference between IQueryable<T> and IEnumerable<T>?

Good books tools c# 3.0 in a NutShell,LinqPad, LINQ in Action

握住你手 2024-08-24 19:48:21
  1. 由于使用了 IQueryable,因此它会过滤数据库中的记录。然而,@Codesleuth 关于 r3 的说法可能是正确的。
  2. 最漂亮的方式是 _ctx.Employees.Skip(5).Take(5),因为没有查询替代 Skip 的方法调用,采取首先等。
  1. It filters records within database since IQueryable is used. However, @Codesleuth can be right concerning r3.
  2. The most pretty-looking way is _ctx.Employees.Skip(5).Take(5), because there is no query alternative to method calls of Skip, Take, First, etc.
好久不见√ 2024-08-24 19:48:21

你怎么知道最后一条记录是什么?什么决定顺序?不能先调用 OrderByDescending 然后获取 FirstOrDefault 吗?也许这会产生更快的查询。

How do you know what the last record is? What determines the order? Can't you make a call to OrderByDescending and then take the FirstOrDefault? Perhaps that would yield a quicker query.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文