How to convert SQL timestamp to a string in Entity Framework?

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;
    }

Answers


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).


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;
}

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.


Need Your Help

ActiveRecord raw sql slower than MySql

mysql ruby-on-rails performance activerecord

I would like that someone help me to understand why executing a raw sql query from ActiveRecord(33.6 s) is less performant that Mysql(14.0 s).

UIPickerView in UITableViewCell showing behind next Cell

ios swift uitableview uipickerview

I have a tableview with a number of custom cells. The problem I am having is that the cell with the pickerview is 'bleeding' behind the neighboring cell.