Reputation: 2055
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;
}
Upvotes: 4
Views: 3927
Reputation: 2055
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;
}
Upvotes: 2
Reputation: 4259
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).
Upvotes: 2