Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Querying a timestamp column from LINQ to SQL

My table has a timestamp column named "RowVer" which LINQ maps to type System.Data.Linq.Binary. This data type seems useless to me because (unless I'm missing something) I can't do things like this:

// Select all records that changed since the last time we inserted/updated.
IEnumerable<UserSession> rows = db.UserSessions.Where
( usr => usr.RowVer > ???? );

So, one of the solutions I'm looking at is to add a new "calculated column" called RowTrack which is defined in SQL like this:

CREATE TABLE UserSession
(
RowVer timestamp NOT NULL,
RowTrack  AS (convert(bigint,[RowVer])),
-- ... other columns ...
)

This allows me to query the database like I want to:

// Select all records that changed since the last time we inserted/updated.
IEnumerable<UserSession> rows = db.UserSessions.Where
( usr => usr.RowTrack > 123456 );

Is this a bad way to do things? How performant is querying on a calculated column? Is there a better work-around?

Also, I'm developing against Sql Server 2000 for ultimate backwards compatibility, but I can talk the boss into making 2005 the lowest common denominator.

like image 203
Wayne Bloss Avatar asked Feb 13 '09 21:02

Wayne Bloss


People also ask

How LINQ queries converted into SQL queries?

LINQ to SQL translates the queries you write into equivalent SQL queries and sends them to the server for processing. More specifically, your application uses the LINQ to SQL API to request query execution. The LINQ to SQL provider then transforms the query into SQL text and delegates execution to the ADO provider.

What you can do with LINQ to SQL?

When the application runs, LINQ to SQL translates into SQL the language-integrated queries in the object model and sends them to the database for execution. When the database returns the results, LINQ to SQL translates them back to objects that you can work with in your own programming language.

Is LINQ to SQL obsolete?

"As long as LINQ to SQL lives under Entity Framework, it's dead.

What is timestamp column in SQL Server?

Timestamp is a synonym for rowversion. Rowversion data type is not a date or time data type. Each database has a counter that is incremented for each insert or update operation that is performed on a table that contains a rowversion column within the database. This counter is the database rowversion.


3 Answers

AS Diego Frata outlines in this post there is a hack that enables timestamps to be queryable from LINQ.

The trick is to define a Compare method that takes two System.Data.Linq.Binary parameters

public static class BinaryComparer
{
 public static int Compare(this Binary b1, Binary b2)
 {
 throw new NotImplementedException();
 }
}

Notice that the function doesn't need to be implemented, only it's name (Compare) is important.

And the query will look something like:

Binary lastTimestamp = GetTimeStamp();
var result = from job in c.GetTable<tblJobs>
             where BinaryComparer.Compare(job.TimeStamp, lastTimestamp)>0
             select job;

(This in case of job.TimeStamp>lastTimestamp)

EDIT: See Rory MacLeod's answer for an implementation of the method, if you need it to work outside of SQL.

like image 57
jaraics Avatar answered Oct 18 '22 21:10

jaraics


SQL Server "timestamp" is only an indicator that the record has changed, its not actually a representation of Date/Time. (Although it is suppose to increment each time a record in the DB is modified,

Beware that it will wrap back to zero (not very often, admittedly), so the only safe test is if the value has changed, not if it is greater than some arbitrary previous value.

You could pass the TimeStamp column value to a web form, and then when it is submitted see if the TimeStamp from the form is different to the value in the current record - if its is different someone else has changed & saved the record in the interim.

like image 26
Kristen Avatar answered Oct 18 '22 21:10

Kristen


// Select all records that changed since the last time we inserted/updated.

Is there a better work-around?

Why not have two columns, one for createddate another for lastmodifieddate. I would say that is more traditional way to handle this scenario.

like image 4
B Z Avatar answered Oct 18 '22 23:10

B Z