We want to create a generic function which will select only required columns rather than returning the whole entity. For example I have a Country class which has the following properties.
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int CountryId { get; set; }
[Required]
public string Name { get; set; }
public int CreatedBy {get;set;}
public DateTime CreatedDate {get;set;}
And I have a Respiratory Class which is common for all entities.
public class Repository<T> : IRepository<T> where T : class
{
DbContext db;
DbSet<T> currentEntity;
public Repository(DbContext db)
{
this.db = db;
currentEntity = db.Set<T>();
}
public void Add(T TEntity)
{
currentEntity.Add(TEntity);
}
public virtual List<T> GetAll()
{
return currentEntity.ToList<T>();
}
}
As GetAll
method is returning all columns, but I want to select only Name
and CountryId
. How can I create a generic function which would be returning only required data?