I currently have the following code:
switch (publicationType) { case PublicationType.Book: return Session.Query<Publication>() .Where(p => p.PublicationType == PublicationType.Book) .OrderByDescending(p => p.DateApproved) .Take(10) .Select(p => new PublicationViewModel { ... }); case PublicationType.Magazine: return Session.Query<Publication>() .Where(p => p.PublicationType == PublicationType.Magazine) .OrderByDescending(p => p.DateApproved) .Take(10) .Select(p => new PublicationViewModel { ... }); case PublicationType.Newspaper .... }
As you can see, the request is the same every time, except for the publishType condition. I tried to reorganize this by creating a method that accepts Func, for example.
private IEnumerable<PublicationViewModel> GetPublicationItems(Func<PublicationType, bool>> pubQuery) { return Session.Query<Publication>() .Where(pubQuery) .OrderByDescending(p => p.DateApproved) .Take(10) .Select(p => new PublicationViewModel { ... }); } private bool IsBook(PublicationType publicationType) { return publicationType == PublicationType.Book; }
and then calling this method, for example
GetPublicationItems(IsBook);
But when I do this, I get the error: InvalidCastException: it is not possible to drop an object like "NHibernate.Hql.Ast.HqlParameter" to enter "NHibernate.Hql.Ast.HqlBooleanExpression".
Is there any other way to do this?
source share