Nhibernate QueryOver Project enum description to string - c#

Let`s say we have the following query using Nhibernate:
var query = session.QueryOver<TenantOrder>(() => tenantOrderAlias)
.JoinAlias(() => tenantOrderAlias.Tenant, () => tenantAlias)
.JoinAlias(() => tenantAlias.Building, () => buildingAlias)
.WhereRestrictionOn(x => tenantOrderAlias.Id.OrderId).IsLike(order.Id);
query.Select(
Projections.Property(() => tenantAlias.Id).WithAlias(() => tenantDto.Id),
Projections.Property(() => tenantAlias.TenantNr).WithAlias(() => tenantDto.TenantNr),
Projections.Property(() => buildingAlias.BuildingNr).WithAlias(() => tenantDto.BuildingNr),
Projections.Property(() => tenantAlias.Floor).WithAlias(() => tenantDto.Floor),
Projections.Property(() => tenantOrderAlias.InstallationStatus).WithAlias(() => tenantDto.InstallationStatusName //? ));
We want to process InstallationStatus (type enum) from tenantOrderalias to InstallationStatusName (property of type string from tenantDto). The structure of the enum looks like below :
public enum TenantInstallationStatusEnum
{
[StringEnum("MS3_TenantInstallationStatus_TS0")]
TS0,//open
[StringEnum("MS3_TenantInstallationStatus_TS2")]
TS2,//abgelesen
}
We want to get the description of the enum and place it to tenantDto.InstallationStatusName. Is this possible using QueryOver or ICriteria ?
We cannot find any post about this issue.

In general, we can project only stuff which exists in a DB side or is passed as a "formula". So, we can convert the status into some conditional statement like this:
// this projection
Projections
.Property(() => tenantOrderAlias.InstallationStatus)
.WithAlias(() => tenantDto.InstallationStatusName //? ));
// could be converted into string values with this statement
Projections
.Conditional(
Restrictions.Where<TenantOrder>(to =>
to.InstallationStatus == TenantInstallationStatusEnum.TS0),
Projections.Constant("MS3_TenantInstallationStatus_TS0"),
Projections.Constant("MS3_TenantInstallationStatus_TS2")
).WithAlias(() => tenantOrderAlias.InstallationStatusName)
);
Conditional could be even nested... but at the end it could be a bit more challenging.
But, maybe more easier in fact, would be to process this conversion ex-post, in C#, on the app side...

Related

automapper optimization mapping (model to view model -> get latest version)

I am trying to optimize this part of code:
Mapper.CreateMap<Document, DocumentViewModel>()
.ForMember(g => g.Id, map => map.MapFrom(d => d.documentVersion.Where(v => v.version == d.documentVersion.Select(s => s.version).Max()).OrderByDescending(s => s.subVersion).First().Id))
.ForMember(g => g.IdRootDocument, map => map.MapFrom(d => d.Id))
.ForMember(g => g.certyficateType, map => map.MapFrom(d => d.documentVersion.Where(v => v.version == d.documentVersion.Select(s => s.version).Max()).OrderByDescending(s => s.subVersion).First().certyficateType))
I'm Using automapper, and I'm trying to optimize this part of code
In this part I'm trying mapping object from document to documentViewModel, in this complex model, source data always will be latest document version:
d => d.documentVersion.Where(v => v.version == d.documentVersion.Select(s => s.version).Max()).OrderByDescending(s => s.subVersion).First().myProportyX
Could anyone offer an example or suggestion as to how to approach optimization in this situation?
d => d.documentVersion.Where(v => v.version == d.documentVersion.Select(s => s.version).Max()).OrderByDescending(s => s.subVersion).First().myProporty
You are iterating quite a few times here, you may consider doing something like:
d.documentVersion.OrderByDescending(v => v.version).ThenByDescending(v => v.subVersion).First().myProperty
to reduce the number of iterations and only get the top version/subversion.
optimization mapping:
Mapper
.CreateMap<Document, DocumentViewModel>()
.ConvertUsing(doc =>
{
DocumentViewModel result = new DocumentViewModel();
DocumentVersion lastVersion = doc.documentVersion.Where(v => v.version == doc.documentVersion.Select(s => s.version).Max()).OrderByDescending(s => s.subVersion).First();
Mapper.Map(lastVersion, result);
return result;
});

MapFrom not always mapping properly

Here's a piece of simple code:
var map = Mapper.CreateMap<Contact, ContactDtoGrid>().
ForMember("executive_account", x => x.MapFrom(y => y.executive_account.Id)).
ForMember("executive_account_Name", x => x.MapFrom(y => y.executive_account.Name));
foreach (Contact c in allContacts)
{
string accountNumber = accounts.Where(x=>x.Key==c.executive_account.Id).SingleOrDefault().Value;
string protocolDescription = (protocolDefinitions.Where(x => x.ProtocolOptionSetValue == c.executive_protocol).Any()) ? protocolDefinitions.Where(x => x.ProtocolOptionSetValue == c.executive_protocol).SingleOrDefault().PortalDescription : string.Empty;
if (protocolDescription != string.Empty)
Console.Write(protocolDescription); //I do get an output so it's not always empty
map.ForMember("executive_account_Number", x => x.MapFrom(y => accountNumber));
map.ForMember("executive_protocol_desc", x => x.MapFrom(y => protocolDescription));
var contactDto = Mapper.Map<Contact, ContactDtoGrid>(c);
}
So all properties map properly, except for executive_protocol_desc which is always an empty string. Like I mentionned in the comment above, this is the case regardless whether protocolDescription is an empty string or not.
Am I doing something obviously wrong ?
Thanks.
Inside the method, you're not really doing a MapFrom, but setting a value. Since automapper is using nested closures, it's just easier to use a direct assignment:
...
var contactDto = Mapper.Map<Contact, ContactDtoGrid>(c);
contactDto.executive_protocol_desc = protocolDescription;
If you need to keep it in the mapper, you should be able to just put the protocol magic as the expression. This would allow you to pull the protocol description out of the foreach loop. Note: I also simplified the linq expression.
map.ForMember("executive_protocol_desc", opt => opt.MapFrom(src =>
protocolDefinitions.Any(pdef => pdef.ProtocolOptionSetValue == src.executive_protocol))
? protocolDefinitions.SingleOrDefault(pdef => pdef.ProtocolOptionSetValue == src.executive_protocol).PortalDescription
: string.Empty)
I took those lines out of the loop:
map.ForMember("executive_account_Number", x => x.MapFrom(y => accountNumber));
map.ForMember("executive_protocol_desc", x => x.MapFrom(y => protocolDescription));
And before the loop kicks in, I changed it to this:
map.ForMember(dest => dest.executive_protocol_desc, opt => opt.MapFrom(x => protocolDefinitions.Where(y => y.ProtocolOptionSetValue == x.executive_protocol.Value).SingleOrDefault().PortalDescription));
map.ForMember(dest => dest.executive_account_Number, opt => opt.MapFrom(x => accounts.Where(y => y.Key == x.executive_account.Id).SingleOrDefault().Value));
Now this maps properly.

How to partially project a child object with many fields in nHibernate

I have the following nHibernate query that select a course based on its course id and then return selected fields for the course object on the initial fetch, and the query executes with no issues.
MatchMode option = ...
CourseItem courseAlias = null;
TeacherItem teacherAlias = null;
var query = session.QueryOver<CourseItem>()
.JoinAlias(c => c.Teacher, () => teacherAlias)
.Where(c => c.CourseID.IsInsensitiveLike(strNumber, option))
.SelectList(list => list
.Select(c => c.CourseID).WithAlias(() => courseAlias.CourseID)
.Select(c => c.IsActive).WithAlias(() => courseAlias.IsActive)
.Select(c => c.CourseDesc).WithAlias(() => courseAlias.CourseDesc)
.Select(c => c.Teacher).WithAlias(() => courseAlias.Teacher))
.TransformUsing(Transformers.AliasToBean<CourseItem>())
.List<CourseItem>();
I wanted to go a step further with the query to only return a partial teacher object, let's say i just wanted to return the ID and Name. So, I updated the projected list to as follows:
var query = session.QueryOver<CourseItem>()
.JoinAlias(c => c.Teacher, () => teacherAlias)
.Where(c => c.CourseID.IsInsensitiveLike(strNumber, option))
.SelectList(list => list
.Select(c => c.CourseID).WithAlias(() => courseAlias.CourseID)
.Select(c => c.IsActive).WithAlias(() => courseAlias.IsActive)
.Select(c => c.CourseDesc).WithAlias(() => courseAlias.CourseDesc)
.Select(c => c.Teacher.ID).WithAlias(() => courseAlias.Teacher.ID)
.Select(c => c.Teacher.Name).WithAlias(() => courseAlias.Teacher.Name))
.TransformUsing(Transformers.AliasToBean<CourseItem>())
.List<CourseItem>();
The query doesn't work because nHibernate has no idea how to resovled based on Teacher.ID and Teacher.Name. Any thoughts on whether it's possible to NOT fetch the entire child object back to a parent object?
I've tried the following query and it works this is not my fully desired outcome
var query = session.QueryOver<CourseItem>(() => courseAlias)
.JoinAlias(() => courseAlias.Teacher, () => teacherAlias)
.Where(() => courseAlias.CourseID.IsInsensitiveLike(strNumber, option))
.SelectList(list => list
.Select(() => courseAlias.CourseID)
.Select(() => courseAlias.IsActive)
.Select(() => courseAlias.CourseDesc)
.Select(() => teacher.ID)
.Select(() => teacher.Name))
.List<object[]>();
I can query the right values but unable to transform it back correctly to the Course / teacher data type.
Any thoughts?
thanks!
We can indeed use custom transformer. There is one, which I am using for a really very very deep projections (inlcuding dynamic objects - 5.1.13. component, dynamic-component)
DeepTransformer<TEntity>
Take it (if needed adjust it) and your final query could be like this
// just the last lines are different
var query = session.QueryOver<CourseItem>()
.JoinAlias(c => c.Teacher, () => teacherAlias)
.Where(c => c.CourseID.IsInsensitiveLike(strNumber, option))
.SelectList(list => list
.Select(c => c.CourseID).WithAlias(() => courseAlias.CourseID)
.Select(c => c.IsActive).WithAlias(() => courseAlias.IsActive)
.Select(c => c.CourseDesc).WithAlias(() => courseAlias.CourseDesc)
// the native WitAlias would not work, it uses expression
// to extract just the last property
//.Select(c => c.Teacher.ID).WithAlias(() => courseAlias.Teacher.ID)
//.Select(c => c.Teacher.Name).WithAlias(() => courseAlias.Teacher.Name))
// so we can use this way to pass the deep alias
.Select(Projections.Property(() => teacherAlias.ID).As("Teacher.ID"))
.Select(Projections.Property(() => teacherAlias.Name).As("Teacher.Name"))
// instead of this
// .TransformUsing(Transformers.AliasToBean<CourseItem>())
// use this
.TransformUsing(new DeepTransformer<CourseItem>())
And in case, that your aliases do match to property names, that transformer will built the object tree...

nhibernate joinalias doesnt build right query

I get SqlException (The multi-part identifier "packageali1_.PackageID" could not be bound.) whenever I try to execute this query (QueryOver):
var brandsFromBrandsInManufacturer2 =
session.QueryOver<Brand>(() => brandAlias)
.JoinAlias(brand => brand.Package, () => packageAlias)
.Where(
brand =>
brand.ArtificialBrand == 0
)
.And(brand => packageAlias.PackageID
.IsIn(branchPackagesProductGroupShortName.Keys))
.Select(brand => brand.BrandName,
brand => packageAlias.PackageID)
List<object[]>();
It looks like I have error in my mappings:
public class PackageMap : ClassMap<Package>
{
public PackageMap()
{
Table("Packages");
Id(x => x.PackageID).GeneratedBy.Identity();
Map(x => x.Aggregated).CustomType<PackageAggregation>();
References(x => x.DataEndPeriod, "DataEndPeriodID");
References(x => x.Country, "CountryID");
References(x => x.ProductGroup, "ProductGroupID");
HasMany(x => x.PackageHierarchies).KeyColumns.Add("PackageId");
HasMany(x => x.Brands).KeyColumns.Add("PackageId").Inverse();
}
}
public BrandMap()
{
Table("Brands");
CompositeId().KeyProperty(x =>x.BrandId).KeyReference(x => x.Package, "PackageId");
Map(x => x.BrandName);
References(x => x.Manufacturer).Columns(x => x.ManufacturerId).Nullable();
Map(x => x.ArtificialBrand);
}
but I can't find what is wrong. Only thing what I've found is fact, that Brand has classic ID key, but Package has composite key.
PS: SQL query from nHibernate is:
SELECT this_.BrandName as y0_, packageali1_.PackageID as y1_ FROM CD.Brands this_ WHERE this_.ArtificialBrand = #p0 and packageali1_.PackageID in (...)
so as you can see, there is no JOIN.
Could someone point me what is wrong?
The solution here would be to "work" directly with the Key property Package, instead of its alias. So, instead of packageAlias.PackageID, let's navigate there directly through the brand.Package.PackageID.
Also, if we've created so many aliases, let's use them
var brandsFromBrandsInManufacturer2 = session
.QueryOver<Brand>(() => brandAlias)
.JoinAlias(() => brandAlias.Package, () => packageAlias) // use brandAlias
.Where(() => brandAlias.ArtificialBrand == 0 )
// no go to Package directly, not via packageAlias
.And(() => brandAlias.Package.PackageID
.IsIn(branchPackagesProductGroupShortName.Keys))
// the same here, the brand.Package...
.Select(brand => brand.BrandName,
brand => brand.Package.PackageID)
List<object[]>();
Well, the second line, is in fact redundant. so it could be like this:
var list = session
.QueryOver<Brand>(() => brandAlias)
.Where(() => brandAlias.ArtificialBrand == 0 )
.And(() => brandAlias.Package.PackageID
.IsIn(branchPackagesProductGroupShortName.Keys))
.Select(brand => brand.BrandName,
brand => brand.Package.PackageID)
List<object[]>();

NHibernate using QueryOver with WHERE IN

I would create a QueryOver like this
SELECT *
FROM Table
WHERE Field IN (1,2,3,4,5)
I've tried with Contains method but I've encountered the Exception
"System.Exception: Unrecognised method call: System.String:Boolean Contains(System.String)"
Here my code
var qOver = _HibSession.QueryOver<MyModel>(() => baseModel)
.JoinAlias(() => baseModel.Submodels, () => subModels)
.Where(() => subModels.ID.Contains(IDsSubModels))
.List<MyModel>();
I've found the solution!! :-)
var qOver = _HibSession.QueryOver<MyModel>(() => baseModel)
.JoinAlias(() => baseModel.Submodels, () => subModels)
.WhereRestrictionOn(() => subModels.ID).IsIn(IDsSubModels)
.List<MyModel>();
You can try something like this:
// if IDsSubModels - array of IDs
var qOver = _HibSession.QueryOver<MyModel>()
.Where(x => x.ID.IsIn(IDsSubModels))
You don't need a join in this situation
This works and is more elegant
var Strings = new List<string> { "string1", "string2" };
var value = _currentSession
.QueryOver<T>()
.Where(x => x.TProperty == value)
.And(Restrictions.On<T>(y=>y.TProperty).IsIn(Strings))
.OrderBy(x => x.TProperty).Desc.SingleOrDefault();
where T is a Class and TProperty is a property of T

Categories

Resources