Create a join between two classes in my repository

0

I have a repository for my PessoaCadastro class that relates to the Pessoa class. I need to create a GetJoinAll(...) method that shows me all records in the PessoaCadastro table that are related to Pessoa through the PessoaId relationship field.

Inshort:"Get all records that are of type People". It would be, more or less, like the SQL below.

I do not handle linq and lambda very well ...

SELECT PC.Id, PC.PessoaTipo, PC.PessoaId, PC.FilialId, P.PessoaNatureza PC.DataInclusao 
FROM PessoaCadastro AS PC 
JOIN Pessoa AS P ON PC.PessoaId = p.PessoaId 
WHERE PC.PessoaTipo = 1

My Repository

using Microsoft.EntityFrameworkCore;
using SistemaComercial.Domain.Interfaces;
using SistemaComercial.Infra.Data.Context;
using System;
using System.Linq;

namespace SistemaComercial.Infra.Data.Repository
{
    public class Repository<TEntity> : IRepository<TEntity> where TEntity : class
    {
        protected readonly SistemaComercialContext Db;
        protected readonly DbSet<TEntity> DbSet;

        public Repository(SistemaComercialContext context)
        {
            Db = context;
            DbSet = Db.Set<TEntity>();
        }

        public virtual void Add(TEntity obj)
        {
            DbSet.Add(obj);
        }

        public virtual TEntity GetById(int id)
        {
            return DbSet.Find(id);
        }

        public virtual IQueryable<TEntity> GetAll()
        {
            return DbSet;
        }

        public virtual void Update(TEntity obj)
        {
            DbSet.Update(obj);
        }

        public virtual void Remove(int id)
        {
            DbSet.Remove(DbSet.Find(id));
        }

        public int SaveChanges()
        {
            return Db.SaveChanges();
        }

        public void Dispose()
        {
            Db.Dispose();
            GC.SuppressFinalize(this);
        }
    }
}

Repository of PessoaCadastro where I need to implement the GetJoinAll(...) function:

using SistemaComercial.Domain.Interfaces;
using SistemaComercial.Domain.Models;
using SistemaComercial.Infra.Data.Context;
using System.Collections.Generic;

namespace SistemaComercial.Infra.Data.Repository
{
    public class PessoaCadastroRepository : Repository<PessoaCadastro>, IPessoaCadastroRepository
    {
        public PessoaCadastroRepository(SistemaComercialContext context)
            :base(context)
        {

        }

        public IEnumerable<PessoaCadastro> GetJoinAll()
        {
            return DbSet.Include...
        }
    }
}
    
asked by anonymous 13.02.2018 / 20:51

2 answers

0

Here's an example using Linq:

 var result = from x in PessoaCadastro
                     join y in Pessoas on new { X1 = x.PessoaId } equals new { X1 = y.PessoaId  }
                     where x.PessoaTipo == 1
                     select y;
        return result.ToList();

Comment if it worked out for you

    
14.02.2018 / 00:32
-1
var lista = Entity<PessoaCadastro>()
            .GetAll()
            .Select(a=>a.Pessoas)
            .Where(a=> a.PessoaTipo == 1 && a.PessoaId == "seuID")
            .ToList();

Or if it is a list of IDs:

var lista = Entity<PessoaCadastro>()
            .GetAll()
            .Select(a=> a.Pessoas)
            .Where(a=> a.PessoaTipo == 1 && listaIDS.Contains(a.PessoaId))
            .ToList();
    
07.04.2018 / 02:16