我有2个实体产品和图片。并非所有图像都是产品图像,图像是下面文件的子类,是我的实体。我需要找到与产品无关的所有图像。我是检索实体的新手,并尝试了很多方法。任何想法或链接将不胜感激。
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
}
答案 0 :(得分:4)
您可以使用Criteria 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>();
其中ProductImageLink是关联表。
应该导致像......这样的查询。
select ... from image img where not exists(select 1 from productimagelink pil where img.image_id = pil.image_id);