9

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?

Salah Akbari
  • 39,330
  • 10
  • 79
  • 109
Shival
  • 234
  • 2
  • 9
  • In C# that would mean you have to return anonymous types or dynamic types from your repository. I don't think you should want that, do you? It may be better to return `IQueryable` instead of `List` so the consumer can apply projections efficiently. – Gert Arnold Jun 24 '17 at 20:27
  • Yes I want to return dynamic types from my repository. Can i use generic delegate like Action<> and pass it to Linq's Select method to create an anonymous type object which will be containing only two properties of the country entity. – Shival Jun 24 '17 at 21:00

2 Answers2

29

First you need added generic method in to generic repository:

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>();
    }

    public ICollection<TType> Get<TType>(Expression<Func<T, bool>> where, Expression<Func<T, TType>> select) where TType : class
    {
        return currentEntity.Where(where).Select(select).ToList();
    }
}

now, you can called this method. Example:

public void SomeService()
{
    var myData = Repository.Get(x => x.CountryId > 0, x => new { x.CountryId, x.Name });
    foreach (var item in myData)
    {
        var id = item.CountryId;
        var name = item.Name;
        // ... 
    }
}

And last - you need runtime create lambda expression and runtime get Required fields. Maybe this post help you : Create a lambda expression with a new anonymous type at runtime, and How do I read an attribute on a class at runtime? p.s. sory for my bad english =)

er-sho
  • 9,581
  • 2
  • 13
  • 26
  • What if I only want to get, for example, the Id Column? I've created the method with this signature public ICollection GetIds(Expression> select) but when I write: private static void DoMigrate(ISource sourceService, IDestination destinationService, IParser parser) where T : class where R : class { ... List addedIds = destinationService.GetIds(x => x.Id); ... } I get 'R doesn't contains a definition for Id' – Rafael Osuna Dominguez Apr 23 '19 at 17:40
  • 1
    Maybe you need write '{ .. where R: HasId }' instead '{ .. where R: class}', where HasId - class with Id property, and also you need use inheritance from HasId for target classes – Alexander Brattsev Apr 29 '19 at 08:12
  • Right! After read a lot about that It was the option I've took – Rafael Osuna Dominguez Apr 30 '19 at 10:43
-3

Declare a DTO class:

public class Getdata
{
    public int Id { get; set; }
    public string Name { get; set; }
}

And use like this..

public virtual List<Getdata> GetAll()
{
    var countrys = db.currentEntity.ToList();
    var data = (from f in countrys
                select new Getdata
                {
                    Id = f.CountryId,
                    Name = f.CountryName
                 }).ToList();
    return data;
}
Salah Akbari
  • 39,330
  • 10
  • 79
  • 109
Vinoth
  • 1
  • 2