4

I need to retrieve the MAX(timestamp) value from a SQL table into a string variable but can't figure out how.

So if my table/entity is set up like so:

**DEPARTMENTS**
Department  varchar(10) PK
LastUpdated timestamp

What would the Linq query look like? I have tried a few iterations but always get an error.

EDIT: Here is an example of what I tried

 var result = (from d in _context.LOG_Departments
                      select d.LastUpdated).Max().SingleOrDefault();

error: "Cannot implicitly convert type 'byte' to 'string'

EDIT Solution:

public string MaxDepartment()
    {

        CPLinkEntities _context = new CPLinkEntities();
        var results = _context.LOG_Departments.Max(t => t.LastUpdated);
        string hex = BitConverter.ToString(results);
        hex =  hex.Replace("-", "");
        return hex;
    }
4
  • share your code which you tried Commented Feb 1, 2014 at 19:10
  • @AshReva, I added an example of what I tried. The timestamp is stored as bunary on the SQL server so it looks like I need to also convert it. Just not that familiar with LINQ. Commented Feb 1, 2014 at 19:22
  • Did you try removing SingleOrDefault()? Max() already gives you single value Commented Feb 1, 2014 at 19:34
  • 1
    Yes, but since the timestamp is stored as a binary on the server I am now getting the cannot convert type byte to string. Commented Feb 1, 2014 at 19:37

3 Answers 3

2

Timestamp is not da datetime thing:

http://technet.microsoft.com/en-us/library/ms182776.aspx

Is a data type that exposes automatically generated, unique binary numbers within a database. rowversion is generally used as a mechanism for version-stamping table rows. The storage size is 8 bytes. The rowversion data type is just an incrementing number and does not preserve a date or a time. To record a date or time, use a datetime2 data type.

You can then use

long timeStampLong = BitConverter.ToInt64(value, 0);

to get a long back (and convert that long into string if you wish).

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks, I am fully aware of what a timestamp is, I am in fact using it for version control and have since asking the question, found and posted my answer as an edit.
OK, but converting into string seems strange. You can have strange characters in that string. Probably bad for displaying. And you probably cannot even compare 2 of those strings for greater then or similar.
I'm using it for version control of lookup tables like Departments (which don't change very often). I am passing the string to a webpage and storing it in local storage, then comparing each page load to it to see if anything in the Departments table has changed and if so I refresh the local storage hash of Departments. I'm not displaying the value at all. I'm going to be repeating this on may lookups and now need to figure out how to dynamically pass in the table name so I don't have to repeat the code. Should be a pretty slick way of updating lookup dropdowns. Thanks.
2

I figured out how to do the conversion.

public string MaxDepartment() {

    CPLinkEntities _context = new CPLinkEntities();
    var results = _context.LOG_Departments.Max(t => t.LastUpdated);
    string hex = BitConverter.ToString(results);
    hex =  hex.Replace("-", "");
    return hex;
}

Comments

-1

it could be something like this:

yourDBContext context=new yourDBContext();
var maxTimestamp = context.yourTable.Max(t => t.Timestamp);

I'd prefer to see this for more detail on working with linq.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.