简体   繁体   English

IN运算符在NHibernate上使用Linq的地方

[英]Where with IN operator using Linq on NHibernate

I have a linq query with NHibernate using Session.Query<T> method and I in this query I Fetch some complex properties and collection properties. 我有一个使用Session.Query<T>方法的NHibernate的linq查询,我在这个查询中获取了一些复杂的属性和集合属性。 I would like to know, how can I add an condition with IN operator from an int[] ? 我想知道,如何从int[]添加一个带有IN运算符的条件? Look my code: 看看我的代码:

public IEnumerable<Product> GetProducts(int[] idCategories) 
{
    // how to add IN condition here or a subquery 
    var query = Session.Query<Product>()
                   .Where(?????)
                   .Fetch(x=>x.Category)
                   .FetchMany(x=>x.Status).ThenFetch(x=>x.Item);

    return query.ToList();
}

I have another method doing a query to get this int[] and I would like to apply it here, or if is there any way to add this subquery on the IN operator, I really appreciate! 我有另一个方法做一个查询来获取这个int[]我想在这里应用它,或者如果有任何方法在IN运算符上添加这个子查询,我真的很感激!

OBS: I could convert int[] to List<int> if its necessary. OBS:如果必要,我可以将int[]转换为List<int>

Edits 编辑

I got this int[] by a query like: 我通过如下查询得到了这个int[]

return session.Query<Category>.Where(...).Select(x => x.Id).ToArray();

My second question is, how could I add this query as a subquery to filter by category? 我的第二个问题是,如何将此查询作为子查询添加到按类别筛选?

Thank you! 谢谢!

You don't really need the IN operator. 你真的不需要IN运算符。 You can just do it like this: 你可以这样做:

.Where(x => idCategories.Contains(x.Category))

Note: Contains is an extension method. 注意: Contains是一种扩展方法。 You need to ensure that you have a using statement for System.Linq , but you probably already have it. 您需要确保您拥有System.Linq的using语句,但您可能已经拥有它。

Take a look on Restrictions, for example 例如,查看限制

var query = Session.Query<Product>()
     .Where(Restrictions.In(Projections.Property<Product>x=>x.Id),idCategories))
     .Fetch(x=>x.Category)
     .FetchMany(x=>x.Status).ThenFetch(x=>x.Item);

About sub query, you need Subqueries class 关于子查询,您需要Subqueries类

var query = Session.Query<Product>()
               .Where(Subqueries.WhereProperty<Product>(x=>x.Id)...)
               .Fetch(x=>x.Category)
               .FetchMany(x=>x.Status).ThenFetch(x=>x.Item);

for more details please look here How to do subqueries in nhibernate? 有关更多详细信息,请查看此处如何在nhibernate中执行子查询?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM