I have the following parent object that contains a collection of child objects. Sections
public class Department { private Iesi.Collections.Generic.ISet<Section> _sections; public Department() { _sections = new HashedSet<Section>(); } public virtual Guid Id { get; protected set; } public virtual string Name { get; set; } public virtual ICollection<Section> Sections { get { return _sections; } } public virtual int Version { get; set; } } public partial class Section { public Section() { _employees = new HashedSet<Employee>(); } public virtual Guid Id { get; protected set; } public virtual string Name { get; set; } public virtual Department Department { get; protected set; } public virtual int Version { get; set; } }
I would like to convert (smooth) it to the next DTO
public class SectionViewModel { public string DepartmentName { get; set; } public string SectionName { get; set; } }
Using the following code.
SectionModel sectionModel = null; Section sections = null; var result = _session.QueryOver<Department>().Where(d => d.Company.Id == companyId) .Left.JoinQueryOver(x => x.Sections, () => sections) .Select( Projections.ProjectionList() .Add(Projections.Property<Department>(d => sections.Department.Name).WithAlias(() => sectionModel.DepartmentName)) .Add(Projections.Property<Department>(s => sections.Name).WithAlias(() => sectionModel.SectionName)) ) .TransformUsing(Transformers.AliasToBean<SectionModel>()) .List<SectionModel>();
However, I get the following exception: Failed to resolve property: Department.Name of: Domain.Section
I even tried the following LINQ expression
var result = (from d in _session.Query<Department>() join s in _session.Query<Section>() on d.Id equals s.Department.Id into ds from sm in ds.DefaultIfEmpty() select new SectionModel { DepartmentName = d.Name, SectionName = sm.Name ?? null }).ToList();
Mappings
public class DepartmentMap : ClassMapping<Department> { public DepartmentMap() { Id(x => x.Id, m => m.Generator(Generators.GuidComb)); Property(x => x.Name, m => { m.Length(100); m.NotNullable(true); }); Set(x => x.Sections, m => { m.Access(Accessor.Field); m.Inverse(true); m.BatchSize(20); m.Key(k => { k.Column("DeptId"); k.NotNullable(true); }); m.Table("Section"); m.Cascade( Cascade.All | Cascade.DeleteOrphans); }, ce => ce.OneToMany()); } } public class SectionMap : ClassMapping<Section> { public SectionMap() { Id(x => x.Id, m => m.Generator(Generators.GuidComb)); Property(x => x.Name, m => { m.Length(100); m.NotNullable(true); }); ManyToOne(x => x.Department, m => { m.Column("DeptId"); m.NotNullable(true); }); } }
But this calls the method or the operation is not implemented .
Looking for guidance on what I'm doing wrong or missing.