我正在尝试使用 ICriteria 向此查询添加过滤器。在应用程序的其余部分中,我们使用了 linq 到 nHibernate,这些过滤器工作正常,但是当我们使用 I 标准时,它们会中断。
日期过滤器以这种方式工作正常,但是当它尝试为操作系统名称设置过滤器时会引发错误。
var query =
this.session.QueryOver<AppEvent>();
if (StartDate.HasValue) query = query.Where(a => a.Time >= StartDate);
if (EndDate.HasValue) query = query.Where(a => a.Time <= EndDate);
if (!string.IsNullOrEmpty(OSVersion)) query = query.Where(a => a.App.Version == OSVersion);
if (!string.IsNullOrEmpty(OSName)) query = query.Where(a=> a.App.OperatingSystemName == OSName);
if (!string.IsNullOrEmpty(Model)) query = query.Where(a => a.Client.ClientInfos.Any(x => x.DeviceModel == Model));
var executedSql = query.Where(a => a.Name == eventName)
.Left.JoinQueryOver<AppEventParameter>(t => t.Parameters)
.Where(r => r.ParameterKey.IsIn(parameters))
.TransformUsing(Transformers.DistinctRootEntity)
.List();
var results = executedSql.Select(a => new DTO
{
param1 = a.Parameters.FirstOrDefault(x => x.ParameterKey == "param1") == null ? "" : a.Parameters.First(x => x.ParameterKey == "param1").ParameterValue,
param2 = a.Parameters.FirstOrDefault(x => x.ParameterKey == "param2") == null ? "" : a.Parameters.First(x => x.ParameterKey == "param2").ParameterValue
}).Distinct(new DTOUniqueComparer());
抛出的错误是:
NHibernate.QueryException : could not resolve property: App.OperatingSystemName of: Domain.Entities.AppEvent
感谢您的提前帮助
编辑:我设法使用 JoinAlias 方法解决了它。在这里查看:
if (StartDate.HasValue) query = query.Where(a => a.Time >= StartDate);
if (EndDate.HasValue) query = query.Where(a => a.Time <= EndDate);
if (!string.IsNullOrEmpty(OSVersion)) query = query.Where(a => a.App.Version == OSVersion);
if (!string.IsNullOrEmpty(OSName)) query = query.JoinAlias(()=>appEventAlias.App,()=>appAlias).Where(()=>appAlias.OperatingSystemName==OSName);
if (!string.IsNullOrEmpty(Model)) query = query.JoinAlias(()=>appEventAlias.Client, ()=>clientAlias).Where(()=>clientAlias.Id==appEventAlias.Client.Id).JoinAlias(()=>clientAlias.ClientInfos, () => clientInfoAlias).Where(()=>clientInfoAlias.DeviceModel == Model);