How to do a search with Join using Entity framework Lambda and Linq

0

How would I do to search two or more tables.

In the system I'm doing, I have the Boat, BarOperationType, and BarBase

My mapping is as follows:

       HasRequired(c => c.ClasseBarco)
            .WithMany(c => c.Barcos)
            .HasForeignKey(c => c.ClasseBarcoId);

       HasRequired(c => c.TipoDeOperacaoDoBarco)
            .WithMany(c => c.Barcos)
            .HasForeignKey(c => c.TipoOperacaoId);

I made a generic repository for searching

    public virtual IEnumerable<TEntity> Buscar(Expression<Func<TEntity, bool>> predicate)
    {
        return Dbset.Where(predicate);
    }

And I use this generic repository to do specific searches for the boats class, here are some examples below:

    public IEnumerable<Barco> ObterAtivos()
    {
        return Buscar(c => c.Ativo && !c.Excluido);          
    }

    public Barco ObterPorNome(string nome)
    {
        return Buscar(c => c.Nome == nome).FirstOrDefault();
    }

And if I want to fetchBarcoeOperation Type andBarcoClass, which are different tables, but which are related to boating table, how would I do?

My idea is to show this data in a table

Follow below how is my bank

    
asked by anonymous 23.10.2018 / 15:43

1 answer

0

Good afternoon.

To perform the search according to the comment placed, the search would be as follows:

public Tipo BuscarPorBarco(int idBarco){
    return Buscar(t => t.Barco.IdBarco == idBarco).FirstOrDefault();
}

This response is given by thinking that in your Tipo class there is a property for relationship between Tipo and Barco , for example:

public virtual ICollection<Barco> Barco { get; set; }
    
25.10.2018 / 17:57