I have a number of tables in my SQL DB that store basic KVP data. they all have the pattern of Id(int), Description(varchar(..)).
I require them all at one stage or another in my MVC application and i am trying to create an anonymous method that will take the type of entity class and return a list of that items data. I would like to be able to use this method for any number of tables in my database without having to extend the methods i am writing by too much.
Example Data:
- Id: 1, Description: Red
- Id: 2, Description: Blue
- Id: 3, Description: Green
ISet Implementation:
public interface ISet
{
int Id { get; set; }
string Description { get; set; }
}
Possible Method Implementation
public static IList<T> BuildSet<T>()
{
using (var db = new somethingEntities())
return db.Set(typeof(T)).Cast<ISet>().Select(c => new { c.Id, c.Description }).ToList();
}
Usage
var data = BuildSet<YourType>();
Runtime Error
Cannot create a DbSet<ISet> from a non-generic DbSet for objects of type 'YourType'.
Does anyone know how to do this?
Thanks.