Nhibernate query to select count of grouped by rows - nhibernate

I need to get this query using NHibernate:
Select RequestStatus.[Status], Count(ApprovalRequest.Id)
From ApprovalRequest Inner Join RequestStatus On ApprovalRequest.CurrentStatusId = RequestStatus.Id
Where RequestStatus.[Status] In ('Approved', 'Queried') And ApprovalRequest.Deleted != 1
Group By RequestStatus.[Status]
here are my classes:
public class ApprovalRequest
{
public ApprovalRequest()
{
Id = Guid.NewGuid();
Statuses = new List<RequestStatus>();
}
/// <summary>
/// Identity No.
/// </summary>
public Guid Id { get; set; }
/// <summary>
/// Represents the current status of the request e.g. Approved.
/// </summary>
public RequestStatus CurrentStatus { get; set; }
/// <summary>
/// The statuses that the request experiences.
/// </summary>
public IList<RequestStatus> Statuses { get; set; }
}
}
public class RequestStatus
{
public RequestStatus()
{
Id = Guid.NewGuid();
}
public Guid Id { get; set; }
public StatusEnum Status { get; set; }
}
I tried this query :
var lst = uow.Session.QueryOver<ApprovalRequest>()
.JoinQueryOver<RequestStatus>(req => req.CurrentStatus)
.SelectList(list =>
list
.SelectCount(p => p.Id)
.SelectGroup(p => p.CurrentStatus.Status)
).List();
but I got an error:
could not resolve property: CurrentStatus.Status of: ApprovalRequest (NHibernate)

You need to bind your joined table to an alias.
ApprovalRequest approvalRequestAlias = null;
RequestStatus requestStatusAlias = null;
var lst = uow.Session.QueryOver<ApprovalRequest>(() => approvalRequestAlias)
.JoinAlias(() => approvalRequestAlias.CurrentStatus, () => requestStatusAlias)
.SelectList(list => list
.SelectCount(p => p.Id)
.SelectGroup(p => requestStatusAlias.Status)
).List();

Related

Projection or Custom Value Resolvers with new Multi Lingual Mappings

I am using the newest version of ASP.Net Boilerplate (version 3.7.2). I am currently using the new Multi-Lingual Entities.
I am having issues with the Automapping for 1 of the Entities to a Dto as not only does it require the Multi-Lingual aspect but a Projection/Custom Resolver for one of the properties.
Up to this point all the mappings have been working correctly. I have followed the documentation found at ASP.NET Boilerplate Documentation
[Table("CategoryItems")]
public class CategoryItem : BaseClass, IMultiLingualEntity<CategoryItemTranslation>
{
/// <summary>
/// Gets or Sets the Category Item Display Order
/// </summary>
public int DisplayOrder { get; set; }
/// <summary>
/// Gets or Sets Catalog Categories
/// </summary>
public virtual ICollection<CatalogItem> CatalogItems { get; set; } = new HashSet<CatalogItem>();
...
/// <summary>
/// Gets or Sets all the Entity Translations
/// </summary>
public virtual ICollection<CategoryItemTranslation> Translations { get; set; } = new HashSet<CategoryItemTranslation>();
}
[Table("CategoryItemTranslations")]
public class CategoryItemTranslation : IEntityTranslation<CategoryItem>
{
/// <summary>
/// Get or Set Category Item Name
/// </summary>
public string CategoryItemName { get; set; }
/// <summary>
/// Gets or Sets Link to Category Item
/// </summary>
public CategoryItem Core { get; set; }
/// <summary>
/// Gets or Sets Link to Core Id
/// </summary>
public int CoreId { get; set; }
/// <summary>
/// Gets or Sets the Language
/// </summary>
public string Language { get; set; }
}
public class ReadCategoryItemFilterDto : PhantomEntityDto
{
public string CategoryItemName { get; set; }
...
public int ItemCount { get; set; }
...
}
Using the Create MultiLingual Map code within the Initialize of the ApplicationModule class:
cfg.CreateMultiLingualMap<CategoryItem, CategoryItemTranslation, ReadCategoryItemFilterDto>(new MultiLingualMapContext(IocManager.Resolve<ISettingManager>()));
The CategoryItemName from the Translation Entity correctly maps to the CategoryItemName on the ReadCategoryItemFilterDto.
Now I need to map the CatalogItems Count to the ItemCount on the Dto.
I have tried adding to the Mapping Configurator:
cfg.CreateMap<CategoryItem, ReadCategoryItemFilterDto>()
.ForMember(dest => dest.ItemCount, opts => opts.MapFrom(src => src.CatalogItems.Count));
and
cfg.CreateMap<CategoryItem, ReadCategoryItemFilterDto>()
.ForMember(dest => dest.ItemCount, opts => opts.ResolveUsing(new CatalogItemFilterCountResolver()));
using
public class CatalogItemFilterCountResolver : IValueResolver<CategoryItem, ReadCategoryItemFilterDto, int>
{
public int Resolve(CategoryItem source, ReadCategoryItemFilterDto destination, int m, ResolutionContext context)
{
return source.CatalogItems.Count;
}
}
If I add this map before the CreateMultiLingualMap the CatalogItemName Maps correctly but the ItemCount fails.
If I put the Projection or Resolver after the CreateMultiLingualMap then the ItemCount maps correctly but the CatalogItemName fails to map.
How do I create a Map Profile to allow for both of the properties to be mapped?
Thank you.
For your case, you don't need to use the extension CreateMultiLingualMap.
You can create your own mapping definition with multilingual mapping included.
That's how it is done;
configuration.CreateMap<CategoryItem, ReadCategoryItemFilterDto>()
.BeforeMap((source, destination, context) =>
{
var translation = source.Translations.FirstOrDefault(pt => pt.Language == CultureInfo.CurrentUICulture.Name);
if (translation != null)
{
context.Mapper.Map(translation, destination);
return;
}
var multiLingualMapContext = new MultiLingualMapContext(IocManager.Instance.Resolve<ISettingManager>());
var defaultLanguage = multiLingualMapContext.SettingManager.GetSettingValue(LocalizationSettingNames.DefaultLanguage);
translation = source.Translations.FirstOrDefault(pt => pt.Language == defaultLanguage);
if (translation != null)
{
context.Mapper.Map(translation, destination);
return;
}
translation = source.Translations.FirstOrDefault();
if (translation != null)
{
context.Mapper.Map(translation, destination);
}
}).ForMember(dest => dest.ItemCount, opts => opts.MapFrom(src => src.CatalogItems.Count));
https://aspnetboilerplate.com/Pages/Documents/Multi-Lingual-Entities#createmultilingualmap
configuration.CreateMultiLingualMap<CategoryItem, CategoryItemTranslation, ReadCategoryItemFilterDto>(context)
.EntityMap.ForMember(dest => dest.ItemCount, opt => opt.MapFrom(src => src.CatalogItems.Count));

Linq Select statement inside Order By

Is it possible to write an linq select inside order by something like :
ReservationDTO obj = new ReservationDTO();
obj.bookroomview = obj.bookroomview.GroupBy(a => a.RoomFloor)
.Select(a => obj.bookroomview
.Select(x => new { Amount = a.Select(b => b.ReservationRoomID).Count(), Name = a.Key })
.OrderBy(x => x.Amount)
).ToList().AsQueryable();
My aim is to select all obj.roombookview items but sort them according to Count calculation. If it is possible how can i write it?
My view class :
public partial class Book_Room_View
{
public Nullable<int> ReservationID { get; set; }
public Nullable<System.DateTime> StartDate { get; set; }
public Nullable<System.DateTime> EndDate { get; set; }
public Nullable<int> ReservationRoomID { get; set; }
public string RoomName { get; set; }
}
if you were to order the groups by reservations, I guess this is the proper way doing it, you don't need to count specific attribute as I guess you are trying, just order by the count of the value list.
obj.bookroomview.GroupBy(a => a.RoomFloor)
.OrderBy(gr => gr.Count())
.Select(gr => new { Amount = gr.Count(), Name = gr.Key })
.AsQueryable();

How to get List of Parent Entities with Child Count In Nhibernate QueryOver

I have two classes :
public class Parent
{
public int Id { get; set; }
public string Name { get; set; }
public ICollection<Child> Childrens { get; set; }
}
public class Child
{
public int Id { get; set; }
public string Name { get; set; }
}
Now through Nhibernate QueryOver I want to get list of all Parent with no of Count of children in single query.
Expected output is ?:
ParentId Name ChildrenCount
1 ABC 10
2 CDE 5
can anyone help me .
Using this DTO for projection:
public class ParentDto
{
public int Id { get; set; }
public string Name { get; set; }
public int ChildrenCount { get; set; }
}
Use this query:
Child childAlias = null;
ParentDto dto = null;
var dtoParents = Session.QueryOver<Parent>()
.JoinAlias(x => x.Childrens, () => childAlias)
.SelectList(list => list
.SelectGroup(x => x.Id).WithAlias(() => dto.Id)
.SelectGroup(x => x.Name).WithAlias(() => dto.Name)
.SelectCount(() => childAlias.Id).WithAlias(() => dto.ChildrenCount))
.TransformUsing(Transformers.AliasToBean<ParentDto>())
.List<ParentDto>();
You can read more about QueryOver projections using DTOs here.

Convert CASE WHEN FROM SQL to NHIBERNATE

I have a SQL statement below that needs to convert it to NHibrnate QueryOver. I have searched the web but can't find a concrete solution. Anyone can help me on this?
SELECT
TABEL1.Id,
CASE WHEN EXISTS (SELECT Id FROM TABLE2 WHERE TABLE2.ID = TABLE1.ID)
THEN 'TRUE'
ELSE 'FALSE'
END AS NewFiled
FROM TABLE1
--Here is the real POCO
public class UserRole
{
[Required]
public virtual User User { get; set; }
[Required]
public virtual Role Role { get; set; }
}
public class UserTenant
{
[Required]
public virtual Tenant Tenant { get; set; }
[Required]
public virtual User User { get; set; }
}
public class Role
{
public int Id {get;set}
[StringLength(255), Required]
public virtual string RoleLabel { get; set; }
[StringLength(4000), Required]
public virtual string RoleDescription { get; set; }
}
public class User
{
public int Id {get;set}
[StringLength(255), Required]
public virtual string Firstname { get; set; }
[StringLength(255), Required]
public virtual string Lastname { get; set; }
}
public class Tenant
{
public int Id {get;set}
[StringLength(255), Required]
public virtual string Name { get; set; }
[StringLength(4000), Required]
public virtual string Description { get; set; }
}
public class AssignRoleUsersModel
{
public virtual int UserId { get; set; }
public virtual string LastName { get; set; }
public virtual string FirstName { get; set; }
public virtual bool IsAssigned { get; set; }
}
--This is the method to get the users assigned or not for that particular role.
public RoleUsers GetRoleUsers(int Id)
{
UserRole userRolesAlias = null;
UserTenant userTenantsAlias = null;
-- This query will get the role depending the Id that have passed. Take note that I don't want to use this query that is why I am converting it to queryOver.
var role = (from r in RoleRepository.Queryable()
where r.Id == Id
select r).FirstOrDefault();
--This query will get all the users having the role result above.Take note that I don't want to use this query that is why I am converting it to queryOver.
var assignedUsers = UserRoleRepository.Queryable().Where(x => x.Role.Id == role.Id).Select(a => a.User.Id).ToArray();
--This is the condition to know if the user was assigned to the role
var projection = Projections.Conditional(Restrictions.Where(() => userRolesAlias.User.Id.IsIn(assignedUsers))
, Projections.Constant(true)
, Projections.Constant(false)
);
var users =
new List<AssignRoleUsersModel>(UnitOfWorkLocalData.CurrentUnitOfWork.Session.QueryOver(() => userTenantsAlias)
.Select(x => x.User.Id)
.Select(x => x.User.LastName)
.Select(x => x.User.FirstName)
.Select(x => x.User.UserName)
.Select(projection)
.TransformUsing(Transformers.AliasToBean<AssignRoleUsersModel>())
.List<AssignRoleUsersModel>());
}
EXTENDED based on the extended question, the way how to do CASE WHEN:
// the ID of searched role, coming as parameter id
int searchedRolId = ...
UserRole userRolesAlias = null;
// this is the SUBQUERY we need, the inner select
var subquery = QueryOver
.Of<UserRole>(() => userRolesAlias)
.Where(() => userRolesAlias.Role.Id == searchedRolId)
.Select(x => userRolesAlias.User.Id);
// here we use NHibernate built in Subqueries tools
var projection = Projections.Conditional(
Subqueries.Exists(subquery.DetachedCriteria) // this is the SUB-SELECT
, Projections.Constant(true)
, Projections.Constant(false)
);
ORIGINAL - This could look like this:
var projection = Projections.Conditional(
NHibernate.Criterion.Expression
.Sql("EXISTS (SELECT Id FROM TABLE2 WHERE TABLE2.ID = {alieas}.ID)")
, Projections.Constant(true)
, Projections.Constant(false)
);
var query = session.QueryOver<Table1>();
ResultDto result = null;
var list = query.SelectList(l => l
.Select(x => x.ID).WithAlias(() => result.ID)
.Select(projection).WithAlias(() => result.DoesExist)
)
.TransformUsing(Transformers.AliasToBean<ResultDto>())
.List<ResultDto>();
The list here contains the set of ResultDtos, with ID and decision if the subtabel TABLE2 record exists. The {alias} will be replaced by NHibernate with the alias of the outert table...
Where this is our DTO for projections:
public class ResultDto
{
public virtual int ID { get; set; }
public virtual bool DoesExist { get; set; }
}

entity framework 4, code-only, relationships

I can't figure out how to solve the following problem.
What i need it a relationship from one base class to another, so that every derived class has a relationship with the same table, called 'Item' in my example.
Since this is just an example it doesn't reflect my program. In the real program the relationship with class Item is in a different namespace. Therefore it can't be in the derived class.
The error:
A key is registered for the derived type 'WebApplication1.Client'. Keys must be registered for the root type 'WebApplication1.Base'.
namespace WebApplication1
{
public class Item
{
public int ItemID { get; set; }
}
public class Base
{
public int ID { get; set; }
public int ItemID { get; set; }
public Item Item { get; set; }
}
public class Client : Base
{
public string Name { get; set; }
private List<Project> _projects = null;
public List<Project> Projects
{
get
{
if (_projects == null)
_projects = new List<Project>();
return _projects;
}
}
}
public class Project : Base
{
public string Name { get; set; }
public int ClientId { get; set; }
public Client Client { get; set; }
}
public class Main
{
public static void Test()
{
ContextBuilder<ObjectContext> ContextBuilder = new ContextBuilder<ObjectContext>();
var itemConfig = new EntityConfiguration<Item>();
itemConfig.HasKey(p => p.ItemID);
itemConfig.Property(p => p.ItemID).IsIdentity();
ContextBuilder.Configurations.Add(itemConfig);
var clientConfig = new EntityConfiguration<Client>();
clientConfig.HasKey(p => p.ID);
clientConfig.Property(p => p.ID).IsIdentity();
clientConfig.Property(p => p.Name);
clientConfig.Relationship(p => p.Item).HasConstraint((p, c) => p.ItemID == c.ItemID);
ContextBuilder.Configurations.Add(clientConfig);
var projectConfig = new EntityConfiguration<Project>();
projectConfig.HasKey(p => p.ID);
projectConfig.Property(p => p.ID).IsIdentity();
projectConfig.Property(p => p.Name);
projectConfig.Relationship(p => p.Item).HasConstraint((p, c) => p.ItemID == c.ItemID);
projectConfig.Relationship(p => p.Client).FromProperty(p => p.Projects).HasConstraint((p, c) => p.ClientId == c.ID);
ObjectContext objCtx = ContextBuilder.Create(new SqlConnection(#"Data Source=(local);Initial Catalog=testa;Integrated Security=SSPI;"));
if (!objCtx.DatabaseExists())
objCtx.CreateDatabase();
}
}
}
Look at how do deal with inheritance mapping here: Link
For basic non-relational proberties and how-to reuse them: https://danielwertheim.wordpress.com/2009/11/29/entity-framework-4-how-to-reuse-mappings-and-add-a-concurrency-token/