LINQ计数..最佳方法 [英] LINQ Count .. best method

查看:138
本文介绍了LINQ计数..最佳方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的公司刚开始使用LINQ,但我仍然对LINQ命令和SQL的抽象性(如果是一个词)有点麻烦,我的问题是

My company has just started using LINQ and I still am having a little trouble with the abstractness (if thats a word) of the LINQ command and the SQL, my question is

  Dim query = (From o In data.Addresses _
                    Select o.Name).Count

在我看来,上面的代码是SQL返回所有行,并且对IQueryable结果中的行数进行计数,所以使用

In the above in my mind, the SQL is returning all rows and the does a count on the number rows in the IQueryable result, so I would be better with

    Dim lstring = Aggregate o In data.Addresses _
    Into Count()

或者我是否在思考LINQ的工作方式?我在家中使用VB Express,所以我看不到正在发送到数据库的实际SQL(我认为),因为我无权访问SQL Profiler

Or am I over thinking the way LINQ works ? Using VB Express at home so I can't see the actual SQL that is being sent to the database (I think) as I don't have access to the SQL profiler

推荐答案

如上所述,它们在功能上是等效的,只是使用查询语法.

As mentioned, these are functionally equivalent, one just uses query syntax.

如我的评论中所述,如果您在 LINQPad 中将以下内容作为VB语句进行评估:

As mentioned in my comment, if you evaluate the following as a VB Statement(s) in LINQPad:

Dim lstring = Aggregate o In Test _
    Into Count()

您可以在生成的SQL输出窗口中获得此信息:

You get this in the generated SQL output window:

SELECT COUNT(*) AS [value]
FROM [Test] AS [t0]

与所评估的以下VB LINQ表达式相同:

Which is the same as the following VB LINQ expression as evaluated:

(From o In Test_
    Select o.Symbol).Count

您将获得完全相同的结果.

You get the exact same result.

这篇关于LINQ计数..最佳方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆