Hi guys, I'm using NHibernate 3.3.0 and I will show my entire problem with
this query
I know how to do these queries:
// *query 1* - get the List<int> or int[]
var idCategories = session.QueryOver<Category>()
.Where(/* condiction here */)
.And(/*another condition here*/)
.And(/* another condition here too */)
.Select(x => x.Id)
.List<int>();
//* query 2* - it's using Query<T>,
// because I do not know how to use Fetch, FetchMany, ThenFetch on
QueryOver, use operator IN to get from array
return session.Query<Product>()
.Fetch(x => x.Supplier)
.FetchMany(x => x.Collection).ThenFetch(x =>
x.PropertyOfCollection)
.Where(x => idCategories.Contains(x.Category.Id))
.OrderByDescendnig(x => x.Name)
.List<Product>();
It works, but this code does 2 hits on database, is there any way to use *query
1* as a subquery on Where method of *query 2* ?
Thank you
On Wed, Jul 18, 2012 at 4:11 AM, Alexander I. Zaytsev <
[email protected]> wrote:
> Hi,
>
> What version of NH are you using?
>
>
> 2012/7/18 mysterd429 <[email protected]>
>
>> Felipe,
>>
>> Sorry for the double reply. The issue I had with the data types, just so
>> you know, occurs when the collection that Contains is being called on is
>> some more complex IEnumerable.
>>
>> IEnumerable<Int32> someIntegers = Enumerable.Range(1, 100).Where(i => i %
>>> 2 == 0 || i % 5 == 0); // Just some arbitrary numbers
>>>
>> session.Query<MyEntityType>().Where(et => someIntegers.Contains(et.Id));
>>>
>>
>> I get an error from NHibernate when running this code: "Failed to convert
>> parameter value from a WhereEnumerableIterator`1 to a Int32"
>>
>> Cheers,
>>
>> Don
>>
>>
>> On Tuesday, July 17, 2012 1:53:18 PM UTC-4, mysterd429 wrote:
>>>
>>> Felipe,
>>>
>>> I just tested a little bit, and I found that the int[] worked just
>>> fine. The SQL that was generated was something like WHERE myId in (@p0,
>>> @p1, @p2), with @p0, @p1, and @p2 being set to whatever was in the array.
>>> The number of parameters changes with the size of the array.
>>>
>>> Cheers,
>>>
>>> Don
>>>
>>> On Tuesday, July 17, 2012 9:52:37 AM UTC-4, Felipe Oriani wrote:
>>>>
>>>> Hi misterd429,
>>>>
>>>> I could convert int[] ni List<int> without problem. THis way using
>>>> .Contains(id) will generate a IN operator ?
>>>>
>>>> Thank you!
>>>>
>>>>
>>>> On Tue, Jul 17, 2012 at 10:34 AM, mysterd429 <> wrote:
>>>>
>>>>> Felipe,
>>>>>
>>>>> I'd do something like .Where(p => idCategories.Contains(p.**IdCategory)),
>>>>> but I find it is sometimes finicky about the type. I use List<int>
>>>>> without
>>>>> a problem.
>>>>>
>>>>> Don
>>>>>
>>>>>
>>>>> On Monday, July 16, 2012 10:50:09 PM UTC-4, Felipe Oriani wrote:
>>>>>
>>>>>> Hello Guys, 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. I would like to know, how cna I add an
>>>>>> condition
>>>>>> with IN operator from an int[] ? 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=>**
>>>>>> xCategory).**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!
>>>>>>
>>>>>> Thank you!
>>>>>>
>>>>>> --
>>>>>> ______________________________****________
>>>>>> Felipe B. Oriani
>>>>>>
>>>>>>
>>>>>> "...Trabalhe quanto puder, tornando-se útil quanto possível..." , por
>>>>>> André Luiz
>>>>>>
>>>>>>
>>>>>> --
>>>>> You received this message because you are subscribed to the Google
>>>>> Groups "nhusers" group.
>>>>> To view this discussion on the web visit https://groups.google.com/d/*
>>>>> *msg/nhusers/-/Hqr2JRy-dmYJ<https://groups.google.com/d/msg/nhusers/-/Hqr2JRy-dmYJ>
>>>>> .
>>>>> To post to this group, send email to [email protected].
>>>>> To unsubscribe from this group, send email to nhusers+unsubscribe@**
>>>>> googlegroups.com <nhusers%[email protected]>.
>>>>> For more options, visit this group at http://groups.google.com/**
>>>>> group/nhusers?hl=en <http://groups.google.com/group/nhusers?hl=en>.
>>>>>
>>>>
>>>>
>>>>
>>>> --
>>>> ______________________________**________
>>>> Felipe B. Oriani
>>>> <http://felipeoriani.com.br/>
>>>> Contato: (19) 9611-8646 / (19) 3421-7850
>>>>
>>>> "...Trabalhe quanto puder, tornando-se útil quanto possível..." , por
>>>> André Luiz
>>>>
>>>> --
>> You received this message because you are subscribed to the Google Groups
>> "nhusers" group.
>> To view this discussion on the web visit
>> https://groups.google.com/d/msg/nhusers/-/3nhQ2HsR7XcJ.
>>
>> To post to this group, send email to [email protected].
>> To unsubscribe from this group, send email to
>> [email protected].
>> For more options, visit this group at
>> http://groups.google.com/group/nhusers?hl=en.
>>
>
> --
> You received this message because you are subscribed to the Google Groups
> "nhusers" group.
> To post to this group, send email to [email protected].
> To unsubscribe from this group, send email to
> [email protected].
> For more options, visit this group at
> http://groups.google.com/group/nhusers?hl=en.
>
--
______________________________________
Felipe B. Oriani
felipeoriani.com.br [email protected]
@felipeoriani<http://www.twitter.com/felipeoriani>
LinkedIn <http://br.linkedin.com/in/felipeoriani>
Facebook<http://www.facebook.com/felipeoriani>
Contato: (19) 9611-8646 / (19) 3421-7850
"...Trabalhe quanto puder, tornando-se útil quanto possível..." , por André
Luiz
--
You received this message because you are subscribed to the Google Groups
"nhusers" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/nhusers?hl=en.