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...
}
}
}