You may use dc.Add(Expresssion.Or(Expression.Eq("i.Id", 2),
Expression.Eq("i.Id", 3))),when you use nh2.x, you may use Restrictions instead of Expression. On Tue, Apr 7, 2009 at 2:27 AM, Cosmo <[email protected]> wrote: > > Hello, > > I've the following many to many relation: File 1 --- * File_Insurer * > --- 1 Insurer. I'm trying to query this relation using the Criteria > API (Active Record) to get Files that meet ALL specified Insurers (Get > all Files where Insurer.Id == 2 AND Insurer.Id == 3). > > I tried many solutions: > > DetachedCriteria dc = DetachedCriteria.For<File>(); > dc.SetResultTransformer(new DistinctRootEntityResultTransformer()); > dc.CreateCriteria("Insurers").Add(Expression.Eq("Id", long.Parse > ("2"))); > dc.CreateCriteria("Insurers").Add(Expression.Eq("Id", long.Parse > ("3"))); > List<File> searchResults = File.FindAll(dc).ToList<File>(); > > That gives me a error "duplicate association path: Insurers". > > Next option: > > DetachedCriteria dc = DetachedCriteria.For<File>(); > dc.SetResultTransformer(new DistinctRootEntityResultTransformer()); > dc.CreateCriteria("Insurers").Add(Expression.And(Expression.Eq("Id", > long.Parse("3")), Expression.Eq("Id", long.Parse("2")))); > List<File> searchResults = File.FindAll(dc).ToList<File>(); > > The result list is empty (but shouldn't). > > Next option with alias: > > DetachedCriteria dc = DetachedCriteria.For<File>(); > dc.SetResultTransformer(new DistinctRootEntityResultTransformer()); > dc.CreateAlias("Insurers", "i").Add(Expression.Eq("i.Id", long.Parse > ("2"))).Add(Expression.Eq("i.Id", long.Parse("3"))); > List<File> searchResults = File.FindAll(dc).ToList<File>(); > > The result list is empty again - strange. > > Next try: > > DetachedCriteria dc = DetachedCriteria.For<File>(); > dc.SetResultTransformer(new DistinctRootEntityResultTransformer()); > List<long> insurerIds = new List<long>(); > insurerIds.Add(2); > insurerIds.Add(3); > dc.CreateCriteria("Insurers").Add(Expression.In("Id", insurerIds)); > List<File> searchResults = File.FindAll(dc).ToList<File>(); > > This works somehow, but the result set contains a all possible options > (OR) - it's not an exact match. > > I'm very confused on this topic. Any help would be highly appreciated. > > Regards, > Jakub > > > > -- Regards, Karron --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
