Erm, first of my examples should have been Restrictions.Eq, not .In,
apologies. Reading your question a couple more times, I believe you're
probably leaning towards the second example. Again, check out chapter 12.4
in the NHibernate reference documentation for more details and other methods
of getting what you want.

On Mon, Apr 27, 2009 at 5:06 PM, Hudson Akridge <hudson.akri...@gmail.com>wrote:

> Your question's more of an NHibernate specific question, so you may have
> accidentally posted to the fluent group instead. ;) In any event, I'll try
> and help however I can here.
> First off, what do you mean in terms of an opposite scenario? The
> description you've given changes the objects and players a little bit. Your
> first example is finding a class, by property, which matches any of the
> values you have in a collection. Your second example, I'm assuming, is
> finding on a class that has a collection, which one of the collections
> values is equal to <SomeValue>.
>
> If that's correct, then it depends on what your collection is of. If it's
> of primitive values, then it should be relatively simple. You should be able
> to do something like:
>
> var myInstance = (MyClass)session.CreateCriteria(typeof(MyClass)
> )
>                                           .Add(Restrictions.In("SomeList",
> SomeValue))
>                                           .UniqueResult();
>
> If it's a collection of entity classes that you have mapped, you'd want to
> do something as is pointed out in chapter 12.4 in the NHibernate Reference
> documents:
>
>  var myInstance = (MyClass)session.CreateCriteria(typeof(MyClass))
>                                           .CreateCriteria("SomeList")
>
>  .Add(Restrictions.Eq("PropertyName", SomeValue))
>                                           .UniqueResult();
>
> What happens here is that the second CreateCriteria call creates a separate
> criteria, that's now operating on the referenced collection. Now, any added
> restrictions will be like sub-queries on that collection's entity class.
>
> Hope that helps!
>
> On Mon, Apr 27, 2009 at 1:06 PM, Francisco Silva <
> francisco.sam...@gmail.com> wrote:
>
>>
>> So if I had a ListOfSomething and I want an instance of MyClass whose
>> SomeProperty value is contained in the ListOfSomething, I would use
>>
>> var myInstance = (MyClass)session.CreateCriteria(typeof(MyClass))
>>                                           .Add(Restrictions.In
>> ("SomeProperty", ListOfSomething))
>>                                           .UniqueResult();
>>
>> Now what if I'm in the opposite scenario?
>> I mean, I have SomeValue and I want an instance of MyClass whose
>> SomeList values contain SomeValue.
>> I would hope that there was something like
>>
>> var myInstance = (MyClass)session.CreateCriteria(typeof(MyClass))
>>                                           .Add(Restrictions.In
>> (SomeValue, "SomeList"))
>>                                           .UniqueResult();
>>
>> But there isn't. What other options do I have?
>> >>
>>
>

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Fluent NHibernate" group.
To post to this group, send email to fluent-nhibernate@googlegroups.com
To unsubscribe from this group, send email to 
fluent-nhibernate+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/fluent-nhibernate?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to