Tuesday, May 3, 2011

How can I set the RecordCount, TOP and NOLOCK with LINQ?

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

What about setting RowCount before the query?

From stackoverflow
  • 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.

  • Here's a good article dealing with TransactionScope:

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

  • 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)
    Lucas : 4th option: settings the isolation level to "read uncommited" through either TransactionScope or directly on the DataContext's connection.
    Marc Gravell : @Lucas - that isn't very granular

0 comments:

Post a Comment