NHibernate Left Outer Join SubClass

I have 2 product and image objects. Not all images are product images, and images are a subclass of the file below, these are my entities. I need to find all images that are not related to the product. I am new to object lookup and have tried many approaches. Any ideas or links would be greatly appreciated.

public class File
    {
        #region Feilds
        public virtual int Id { get; set; }
        public virtual string Name { get; set; }
        public virtual Enumerations.File.FileType Type { get; set; }
        public virtual string Extension { get; set; }
        public virtual string Path { get; set; }
        public virtual DateTime DateCreated { get; set; }
        public virtual DateTime DateModified { get; set; }
        #endregion
    }

public class Image : File
{
    #region Fields
    public virtual string ImageName { get; set; }
    public virtual string Description { get; set; }
    public virtual bool Active { get; set; }
    public virtual DateTime DateTaken { get; set; }
    #endregion
}

public class Product
{
    #region Properties
    public virtual int Id { get; set; }
    public virtual string Name { get; set; }
    public virtual string Description { get; set; }
    public virtual decimal Price { get; set; }
    public virtual decimal Weight { get; set; }
    public virtual bool IsDigital { get; set; }
    public virtual DateTime DateCreated { get; set; }
    public virtual IList<Category> ProductCategories { get; set; }
    public virtual IList<ProductAttribute> ProductAttributes { get; set; }
    public virtual IList<Image> ProductImages { get; set; }
    #endregion
}

      

+2


source to share


1 answer


You can use the subdirectory Crititia not exists ...

IList<Image> images = session.CreateCriteria<Image>("img")
        .Add(Expression.Not(Subqueries.Exists(DetachedCriteria.For<ProductImageLink>("pil")
                    .SetProjection(Projections.Constant(1))
                    .Add(Expression.EqProperty("img.image_id", "pil.image_id")))))
        .List<Image>();

      

Where ProductImageLink is the association table.



You should be prompted like ...

select ... from image img where not exists(select 1 from productimagelink pil where img.image_id = pil.image_id);

      

+4


source







All Articles