在C#中,要使用LINQ将SQL中的Timestamp数据类型值转换为Datetime格式,可以使用以下步骤:
using System.Data.Linq;
using System.Data.Linq.Mapping;
[Table(Name = "YourTableName")]
public class YourTable
{
[Column(IsPrimaryKey = true)]
public int Id { get; set; }
[Column]
public byte[] Timestamp { get; set; }
}
public List<YourTable> GetYourTableData()
{
using (var db = new DataContext("YourConnectionString"))
{
var query = from t in db.GetTable<YourTable>()
select new YourTable
{
Id = t.Id,
Timestamp = t.Timestamp
};
return query.ToList();
}
}
var data = GetYourTableData();
foreach (var item in data)
{
DateTime timestamp = new DateTime(BitConverter.ToInt64(item.Timestamp, 0));
Console.WriteLine($"Id: {item.Id}, Timestamp: {timestamp}");
}
这样,就可以将SQL中的Timestamp数据类型值转换为Datetime格式,并在C#中使用LINQ进行查询。
领取专属 10元无门槛券
手把手带您无忧上云