SQLite赢得';t匹配从数据库检索到的C#DateTime
我试图在内存中使用SQLite对我的应用程序运行一些单元测试,但遇到了一个奇怪的问题: 我有两个问题。第一个查询的结果是给定列表名称的最新价目表的日期,第二个查询使用SQLite赢得';t匹配从数据库检索到的C#DateTime,c#,nhibernate,sqlite,C#,Nhibernate,Sqlite,我试图在内存中使用SQLite对我的应用程序运行一些单元测试,但遇到了一个奇怪的问题: 我有两个问题。第一个查询的结果是给定列表名称的最新价目表的日期,第二个查询使用DateTime,以获取最新的价格。问题是,第二个查询不返回任何结果 你知道这里的背景可能出了什么问题吗 var effective = DbSession.Current.CreateCriteria<ItemPrice>() .SetProjection(Project
DateTime
,以获取最新的价格。问题是,第二个查询不返回任何结果
你知道这里的背景可能出了什么问题吗
var effective = DbSession.Current.CreateCriteria<ItemPrice>()
.SetProjection(Projections.Max("Effective"))
.Add(Restrictions.Le("Effective", workDate))
.CreateCriteria("PriceList")
.Add(Restrictions.Eq("ListName", listName))
.Add(Restrictions.Eq("Active", true))
.UniqueResult<DateTime>();
return DbSession.Current.CreateCriteria<ItemPrice>()
.Add(Restrictions.Eq("Effective", effective))
.CreateCriteria("PriceList")
.Add(Restrictions.Eq("ListName", listName))
.Add(Restrictions.Eq("Active", true))
.List<ItemPrice>();
尝试为该列设置时间戳的
CustomType
。我认为,这是保存毫秒与否的区别。尝试为该列设置时间戳的CustomType
。我认为,这是节省毫秒与否的区别。这似乎不起作用,但感谢您为我指明了正确的方向。这似乎不起作用,但感谢您为我指明了正确的方向。您的结果不会编译。调用NullSafeGet时未定义Generates error dateString。谢谢,似乎我对从我开始使用的示例IUserType中删除无关注释有点过分热情。您的结果将无法编译。调用NullSafeGet时未定义Generates error dateString。谢谢,从我开始使用的示例IUserType中删除不相关的注释似乎有点过分了。
class SQLiteDateTime : IUserType
{
#region IUserType Members
public object Assemble(object cached, object owner)
{
return cached;
}
public object DeepCopy(object value)
{
var dt = (DateTime) value;
return new DateTime(dt.Year, dt.Month, dt.Day, dt.Hour, dt.Minute, dt.Second);
}
public object Disassemble(object value)
{
return String.Format("{0:yyyy'-'MM'-'dd' 'HH':'mm':'ss.fff}", value);
}
public new bool Equals(object x, object y)
{
return x.Equals(y);
}
public int GetHashCode(object x)
{
return x.GetHashCode();
}
public bool IsMutable
{
get { return false; }
}
public object NullSafeGet(IDataReader rs, string[] names, object owner)
{
string dateString = (string)NHibernateUtil.String.NullSafeGet(rs, names[0]);
DateTime result = DateTime.ParseExact(dateString, "yyyy'-'MM'-'dd' 'HH':'mm':'ss.fff", CultureInfo.InvariantCulture.DateTimeFormat);
return result;
}
public void NullSafeSet(IDbCommand cmd, object value, int index)
{
if (value == null)
{
NHibernateUtil.String.NullSafeSet(cmd, null, index);
return;
}
value = Disassemble(value);
NHibernateUtil.String.NullSafeSet(cmd, value, index);
}
public object Replace(object original, object target, object owner)
{
return original;
}
public Type ReturnedType
{
get { return typeof (DateTime); }
}
public NHibernate.SqlTypes.SqlType[] SqlTypes
{
get {
var types = new SqlType[1];
types[0] = new SqlType(DbType.String);
return types;
}
}
#endregion
}