English 中文(简体)
NHibernate Search in a List using ICriteria
原标题:

I have my class X :

public class ClassX
{
   public virtual IList<ClassY> ListY { get; set; }
   ...
}

My ClassX mapping (using Fluent)

 ...
 HasMany<ClassX>(x => x.ListY )
        .Inverse()
        .Cascade.AllDeleteOrphan()
        .KeyColumns.Add("`IDX`");
 ...

My Y class:

 public class ClassY
 {
     ...
     public virtual ClassZ Z{ get; set; }
     ...
 }

Mapping ClassY

 ...
 References(x => x.Z, "IDZ").Cascade.None();
 ...

Now, I´d like to search all ClassX elements that have in ListY Z.ID = 2...

How can I do that using ICriteria?

Thanks

最佳回答

With a criteria ?

Session.CreateCriteria<ClassX>()    
   .CreateCriteria("ListY")
   .Add(Expression.Eq("Id", 2)
   .List<ClassX>();

Be aware that you can have only one CreateCriteria, yu can use alias to add restrictions on other properties.

Update after your comment :

oh my mistake, I think you need maybe to use HQL :

select x fron ClassX as x 
left join x.ListY listY
left join listY.Z z
where z.Id=2

Because I m not sure that this will work :

Session.CreateCriteria<ClassX>()    
       .CreateCriteria("ListY")
       .CreateAlias("Z", "z")
       .Add(Expression.Eq("z.Id", 2)
       .List<ClassX>();
问题回答

I think either of this should work:

Session.CreateCriteria<ClassX>()    
       .CreateAlias("ListY", "y")
       .CreateAlias("y.Z", "z")
       .Add(Expression.Eq("z.Id", 2))
       .List<ClassX>();

Session.CreateCriteria<ClassX>()    
       .CreateAlias("ListY", "y")
       .Add(Expression.Eq("y.Z.Id", 2))
       .List<ClassX>();

It may help your understanding if we break this criteria search down into steps. To my knowledge there are two solutions here. The first option is to create an ICriteria object for your other two classes. It would look like this:

ICriteria classXCriteria = Session.CreateCriteria<Class>();
ICriteria classYCriteria = classXCriteria.CreateCriteria<classY>();
ICriteria classZCriteria = classYCriteria.CreateCriteria<classZ>();

classZCriteria.Add(Expression.Eq("Z", [id to search on goes here]));

IList<ClassX> results = classXCriteria.List<ClassX>();

The second option to to use aliases:

ICriteria classXCriteria = Session.CreateCriteria<ClassX>();
classXCriteria.CreateAlias("ListY", "classY");
classXCriteria.CreateAlias("classY.Z", "classZ");
classXCriteria.Add(Expression.Eq("classZ.Id", [id to search on goes here]));

IList<ClassX> results = classXCriteria.List<ClassX>();




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签