I am trying to add filters to this query using ICriteria. Through the rest of the app, we have used linq to nHibernate, and these filters work fine, but when we use I criteria they break.
The date filters work ok this way, but the error is thrown when it tries to set the filter for the operating system name.
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());
The error that is thrown is:
NHibernate.QueryException : could not resolve property: App.OperatingSystemName of: Domain.Entities.AppEvent
Thanks for your help in Advance
EDIT: I managed to solve it using the JoinAlias method. Check it out here:
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);
I managed to solve it. It ended up being the need to use the JoinAlias Method. Here is how I solved the filters:
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);