Domanda

Sto usando NHibernate, e sto cercando di capire come scrivere una query, che searchs tutti i nomi dei miei soggetti, e gli elenchi dei risultati. Come semplice esempio, ho i seguenti oggetti;

public class Cat {
public string name {get; set;}
}

public class Dog {
    public string name {get; set;}
}

public class Owner {
    public string firstname {get; set;}
    public string lastname {get; set;}
}

eventaully Voglio creare una query, diciamo per esempio, che e restituisce tutti i proprietari di animali domestici con un nome che contiene "Ted", o animali domestici con un nome contenente "ted".

Ecco un esempio di SQL che voglio eseguire:

SELECT TOP 10 d.*, c.*, o.* FROM owners AS o
INNER JOIN dogs AS d ON o.id = d.ownerId 
INNER JOIN cats AS c ON o.id = c.ownerId
WHERE o.lastname like '%ted%' 
OR o.firstname like '%ted%' 
OR c.name like '%ted%' 
OR d.name like '%ted%' 

Quando lo faccio usando criteri come questo:

    var criteria = session.CreateCriteria<Owner>()
        .Add(
        Restrictions.Disjunction()
            .Add(Restrictions.Like("FirstName", keyword, MatchMode.Anywhere))
            .Add(Restrictions.Like("LastName", keyword, MatchMode.Anywhere))
        )
        .CreateCriteria("Dog").Add(Restrictions.Like("Name", keyword, MatchMode.Anywhere))
        .CreateCriteria("Cat").Add(Restrictions.Like("Name", keyword, MatchMode.Anywhere));
        return criteria.List<Owner>();

viene generata la seguente query:

   SELECT TOP 10 d.*, c.*, o.* FROM owners AS o
   INNER JOIN dogs AS d ON o.id = d.ownerId 
   INNER JOIN cats AS c ON o.id = c.ownerId 
   WHERE o.lastname like '%ted%' 
   OR o.firstname like '%ted%' 
   AND d.name like '%ted%'
   AND c.name like '%ted%'

Come faccio a modificare la mia domanda in modo che la .CreateCriteria ( "Dog") e .CreateCriteria ( "Gatto") generano una o al posto di AND?

grazie per il vostro aiuto.

È stato utile?

Soluzione

Prova questo, potrebbe funzionare.

var criteria = session.CreateCriteria<Owner>()
            .CreateAlias("Dog", "d")
            .CreateAlias("Cat", "c")
            .Add(
            Restrictions.Disjunction()
                .Add(Restrictions.Like("FirstName", keyword, MatchMode.Anywhere))
                .Add(Restrictions.Like("LastName", keyword, MatchMode.Anywhere))
                .Add(Restrictions.Like("c.Name", keyword, MatchMode.Anywhere))
                .Add(Restrictions.Like("d.Name", keyword, MatchMode.Anywhere))
            );

Altri suggerimenti

È necessario combinare i due criteri che utilizzano Expression.Or (Criteria1, Criteria2)

Maggiori informazioni qui: http://devlicio.us/blogs/derik_whittaker/archive/2009/04/21/creating-a-nested-or-statement-with-nhibernate-using-the- criteri-convention.aspx

Hmm penso che sarebbe simile a questa (preso in prestito un po 'dal codice di BuggyDigger)

var criteria = session.CreateCriteria<Owner>()
    .CreateAlias("Dog", "d")
    .CreateAlias("Cat", "c")
    .Add(Expression.Or(Expression.Like("c.Name", keyword, MatchMode.Anywhere)
            , Expression.Like("d.Name", keyword, MatchMode.Anywhere))
        );

Ma non ho notato che si voleva O tutto. In questo caso l'aggiunta di questi criteri per la disgiunzione, come ha mostrato BuggyDigger, è probabilmente la strada da percorrere.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top