NHibernate QueryOver实体具有IList属性子属性

时间:2017-12-16 19:14:53

标签: c# nhibernate linq-to-entities linq-to-nhibernate

拥有以下课程;

public class Customer{
  ....
  ....
  IList<Receipt> Receipts { get; set; }
}

public class Receipt{
  ....
  IList<SoldProduct> Products { get; set; }
}

说到这里,我的问题是我正在尝试查询已购买特定产品的客户。当我尝试执行以下代码时,我得到一个NullReferenceException。

Customer c = null;
Receipt r = null;
SoldProduct sP = null;

var queryOver = Session.QueryOver(() => c)
    .JoinAlias(() => c.Receipts, () => r)
    .JoinAlias(() => r.SoldProducts, () => sP)
    .Where(() => c.Name.IsLike(query.Search, MatchMode.Anywhere) ||
                           c.Surname.IsLike(query.Search, MatchMode.Anywhere) ||
                           c.Address.IsLike(query.Search, MatchMode.Anywhere) ||
                           c.Receipts.Select(receipt => 
                           receipt.SoldProducts.Select(product => product.Product.OldId.ToString()))
                           .SingleOrDefault().Single().IsLike(query.Search, MatchMode.Anywhere))

我现在就被困住了。我可能会错过这里的关键点,如果是这样,请告诉我。如果实际上有更简单的方法来执行此查询,我真的很感激任何帮助。

1 个答案:

答案 0 :(得分:0)

尝试将过滤器应用于产品别名:

Customer c = null;
Receipt r = null;
SoldProduct sP = null;
Product p = null;

var queryOver = Session.QueryOver(() => c)
    .JoinAlias(() => c.Receipts, () => r)
    .JoinAlias(() => r.SoldProducts, () => sP)
    .JoinAlias(() => sp.Product, () => p)
    .Where(() => c.Name.IsLike(query.Search, MatchMode.Anywhere) ||
                           c.Surname.IsLike(query.Search, MatchMode.Anywhere) ||
                           c.Address.IsLike(query.Search, MatchMode.Anywhere) ||
                           p.OldId.ToString().IsLike(query.Search, MatchMode.Anywhere))