views:

49

answers:

1

Hello guys...

I have a Client class like that:

public class Client
{
    public Person Pers { get; set; }
}

And I have 2 Person´s child class :

public class PersonType1 : Person 
{
   protected string att1;
   protected string att2;
}

public class PersonType2 : Person 
{
     protected string att3;
     protected string att4;
}

public class Person 
{
     protected string attx;
     protected string atty;
}

So, my Client could be PersonType1 or PersonType2...

I need execute a Client search ... The parameters to that search are att1,att2,att3,att4,attx,atty... But all those are optional...

I´m trying to execute that search with ICriteria, but I dont know how specify that inheritance scheme...

+1  A: 

You really cannot do this because at the criteria level the Person class is agnostic of any derived classes. The result set will be an IList<Person> even though iterating through the collection will show you that the elements will also comprise of PersonType1 and PersonType2 types (assume that we just fetch the whole set from the DB without any restrictions).

That said, you can achieve the desired effect with a workaround: Define each derived type as a new subquery

var dc1 = DetachedCriteria.For(typeof(PersonType1)).SetProjection(Projections.Property("id")).Add(Expression.Eq("att1", "foo"));

var dc2 = DetachedCriteria.For(typeof(PersonType2)).SetProjection(Projections.Property("id")).Add(Expression.Eq("att3", "bar"));

for N derived types and then on your main Criteria query just do

CreateCriteria(typeof(Person)).Add(Subqueries.PropertyIn("Id", dc1) || Subqueries.PropertyIn("Id", dc2));

alternatively, an ISQLQuery has not such limitations.

EDIT i'm adding below the necessary adjustments to find Client i will re-write the criteria to be a <Client> criteria and express the subquery restriction on the joined <Person>s.

var crit = ses.CreateCriteria(typeof(Client)).CreateCriteria("Person","per").Add(Subqueries.PropertyIn("per.Id", dc1) || Subqueries.PropertyIn("per.Id", dc2));
Jaguar
Thanks Jaguar...I´ve never use DetachedCriteria, so I´m a bit lost ...I´m not trying to search Person, but the Clients that have determined Person... Can you show me how do that?Thanks
Paul