我有以下型号:
create table Products (
Id UNIQUEIDENTIFIER not null,
Name NVARCHAR(255) null,
CategoryId UNIQUEIDENTIFIER not null,
primary key (Id),
unique (Name, CategoryId)
)
create table Rates (
Id UNIQUEIDENTIFIER not null,
Price NUMERIC(19,5) not null,
TimeStamp DATETIME not null,
UserId UNIQUEIDENTIFIER not null,
ProductId UNIQUEIDENTIFIER not null,
primary key (Id),
unique (Price, UserId, ProductId)
)
create table Categories (
Id UNIQUEIDENTIFIER not null,
Name NVARCHAR(255) not null unique,
primary key (Id)
)
编辑:域模型如下:
public class Category
{
public string Name { get; set; }
public IList<Product> Products { get; set; }
public Guid Id { get; set; }
}
public class Product
{
public string Name { get; set; }
public Category Category { get; set; }
public IList<Rate> Rates { get; set; }
public Guid Id { get; set; }
}
public class Rate
{
public User User { get; set; }
public Product Product { get; set; }
public decimal Price { get; set; }
public DateTime TimeStamp { get; set; }
public Guid Id { get; set; }
}
我想要做的是选择所有没有关联产品的类别和相关费率。即在SQL中,这看起来像:
select * from Categories category where
(select count(*)
from Products product
inner join Rates rate on rate.ProductId = product.Id
where product.CategoryId = category.Id) = 0;
如何使用QueryOver API执行此操作?
答案 0 :(得分:0)
我无法检查生成的sql,但你可以试试这个,也许是有帮助的:
Product product = null;
Rate rate = null;
_session.QueryOver<Category>()
.JoinAlias(category => category.Products, () => product)
.JoinAlias(() => product.Rate, () => rate)
.Where(Restrictions.Eq(Projections.Count(() => product.Id), 0)
.List<Category>();
答案 1 :(得分:0)
找到解决方案:
Category categoryAlias = null;
session.QueryOver<Category>(() => categoryAlias)
.WithSubquery
.WhereNotExists(QueryOver.Of<Product>()
.Where(product => product.Category.Id == categoryAlias.Id)
.JoinQueryOver<Rate>(product => product.Rates)
.Select(product => product.Category))
.List<Category>();