简体   繁体   中英

Linq-to-sql speed when getting table count

Is this:

Dim mydata = (From c In dc.Table Select New With {c.ID}).Count

any faster than this:

Dim mydata = (From c In dc.Table Select c).Count

assuming the table has a good number of fields?

The short answer: no.

The SQL generated by the LINQ-to-SQL engine should be essentially the same for both forms (if not exactly the same) because you're calling .Count() immediately on the query.

A compiled query version, on the other hand, would be faster after the first execution. Here's one way to do a compiled query for this:

Public Shared FetchCount As Func(Of DataContext, Integer) = _
    CompiledQuery.Compile(Function(context as DataContext) _
        (From c in context.Table Select c).Count())

DataContext would need to be the Type of the LINQ-to-SQL DBML, and Table would need to be the appropriate table. And, I believe you'll need to import System.Data.Linq to have access to CompiledQuery .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM