IQUERY没有运行预期
问题描述:
我有以下方法:IQUERY没有运行预期
ServiceType GetWithValidServices(long ServiceTypeID);
及以下功能:
public ServiceType GetWithValidServices(long ServiceTypeID)
{
IQuery query = CurrentSession.CreateQuery("select dl.ServiceType from Service as dl"
+ "where dl.ServiceType.ID = :id and dl.IsValid = true"
+ "order by dl.HavePriority desc");
query.SetInt64("id", ServiceTypeID);
var dlArt = query.UniqueResult<ServiceType>();
return dlArt;
}
在下面的方法我称之为上面提到的功能:
public ServiceCollection GetService(long ServiceTypeID)
{
ServiceType d = DomainToWebgridMapper.GetWebgridServiceType(DaoFactory.Instance.ServiceTypeDao.GetWithValidService(ServiceTypeID));
return d.Service;
}
我的问题是查询运行不正确。我可以看到服务,但dl.IsValid没有运行的过滤器,也没有按照优先级排序。
我在少数其他方法中使用where子句,并且它工作正常。
我不知道这里出了什么问题。也许有人可以帮助我。
在此先感谢
答
我想我明白你的问题是什么;它与你如何连接字符串来创建查询有关。考虑这个了片刻:
string query = "select dl.ServiceType from Service as dl"
+ "where dl.ServiceType.ID = :id and dl.IsValid = true"
+ "order by dl.HavePriority desc";
因为你不插入在开始任何空格或换行/你的字符串,端部你的查询变成是这样的:
select dl.ServiceType from Service as dlwhere dl.ServiceType.ID = :id and dl.IsValid = trueorder by dl.HavePriority desc
^^^^^^^ ^^^^^^^^^
看到我标记了问题?为了解决这个问题,除了最后一个连接起来组成查询的字符串之外,在所有字符的末尾添加一个额外的空格,或者改为使用类似verbatim string literal的东西。
IQuery query = CurrentSession.CreateQuery(
"select dl.ServiceType from Service as dl " +
"where dl.ServiceType.ID = :id and dl.IsValid = true " +
"order by dl.HavePriority desc"
);
谢谢,我已经试过了,但查询仍然没有正确运行 – Paks 2012-07-26 10:51:47