以下是我的域名模型的一个小摘录:
public class Chain
{
public IList<Product> Products { get; set; }
}
public class Store
{
public Chain Chain { get; set; }
public IList<Product> Products { get; set; }
}
现在,我需要在Product
和相关Store
的{{1}}上创建一个查询。
问题是如何扩展存储在归属Chain
中的产品的查询?
这是我到目前为止所做的:
Chain
我该怎么做?
请注意:var subQuery = QueryOver.Of<Store>()
.Where(s => s.Id == searchCriteria.StoreId)
.Select(s => s.Id);
Store storeAlias = null;
var query = _session.QueryOver<Product>()
.JoinAlias(p => p.Stores, () => storeAlias)
.WithSubquery.WhereProperty(() => storeAlias.Id).In(subQuery);
//// some more clauses...
中的Chain
属性可以是Store
。
答案 0 :(得分:1)
我相信这应该有效。我没有在本地数据库上尝试它,但应该让你朝着正确的方向。
Product product = null;
Store store = null;
Chain chain = null;
//01. Load Products from Store
var productsFromStores
= QueryOver.Of(() => product)
.JoinAlias(() => product.Stores, () => store)
.Where(() => store.Id == searchCriteria.StoreId)
.Select(Projections.Distinct(Projections.Id()));
//02. If Chain DOES NOT refer Store
var productFromChains
= QueryOver.Of(() => store)
.JoinAlias(() => store.Chain, () => chain)
.JoinAlias(() => chain.Products, () => product)
.Where(() => store.Id == StoreId)
.Select(Projections.Distinct(
Projections.Property(() => product.Id)));
//03. Load Products from present either in the Store and or the chains
var products
= session.QueryOver(() => product)
.Where(
Restrictions.Or(
Subqueries.WhereProperty(() => product.Id)
.In(productsFromStores),
Subqueries.WhereProperty(() => product.Id)
.In(productFromChains)));
仅供参考:请注意,这可能不是处理查询的最理想方式。当我看到使用IN ()
和子查询时,我总是畏缩不前。
如果Chain DOES中有一个商店,则// 02可以写为
//Load Products from Chains
//02. If Chain DOES refer to a Store
var productFromChains
= QueryOver.Of(() => chain)
.JoinAlias(() => chain.Store, () => store)
.JoinAlias(() => chain.Products, () => product)
.Where(() => store.Id == searchCriteria.StoreId)
.Select(Projections.Distinct(
Projections.Property(() => product.Id)));