tags:

views:

441

answers:

3

Is it possible to write a select statement that also has NOLOCK and TOP in LINQ?

What about setting RowCount before the query?

+2  A: 

Adding .Take(5) to your query is the same as Top 5

Adding .Skip(5) will start returning results on row 6

Not sure how you would add a NOLOCK to a query.

TGnat
+1  A: 

Here's a good article dealing with TransactionScope:

http://www.madprops.org/blog/linq-to-sql-and-nolock-hints/

John Rasch
+1  A: 

TGnat notes how to do TOP (via .Take(n)). However, there is no inbuilt way of adding granular hints (etc) like (NOLOCK) to specific tables. There are a few ways of getting around this:

  • write a stored procedure and expose it via the data-context
  • write a udf and expose it via the data-context
  • use ExecuteQuery with (parameterized) TSQL that has a NOLOCK

Of the 3, the UDF would get my vote:

  • it doesn't leave you with literal queries in the C#
  • the metadata is strongly defined
  • it is composable (with .Skip/.Take/.Where etc)
Marc Gravell
4th option: settings the isolation level to "read uncommited" through either TransactionScope or directly on the DataContext's connection.
Lucas
@Lucas - that isn't very granular
Marc Gravell