查询从LINQ到SQL的时间戳列 [英] Querying a timestamp column from LINQ to SQL

查看:98
本文介绍了查询从LINQ到SQL的时间戳列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的表有一个名为"RowVer"的时间戳列,LINQ将其映射为System.Data.Linq.Binary类型.这种数据类型对我来说似乎毫无用处,因为(除非我丢失了某些东西)我不能做这样的事情:

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 > ???? );

因此,我正在寻找的解决方案之一是添加一个新的称为RowTrack的计算列",该列在SQL中是这样定义的:

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?

此外,我正在针对最终的向后兼容性开发Sql Server 2000,但我可以说服老板使2005成为最低的公分母.

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.

推荐答案

//选择自上次插入/更新以来所有已更改的记录.

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

有更好的解决方法吗?

为什么没有两列,一列用于createddate,另一列用于lastmodifieddate.我会说这是处理这种情况的更传统的方法.

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

这篇关于查询从LINQ到SQL的时间戳列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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