If you can define an interface for your table
public interface IMyTable<T>
{
string Name { get; set; }
T GetAll();
}
then this would compile
public List<T> GetSelection<T>(string a, Expression<Func<IMyTable<T>, T>> columnsToSelect = null)
{
IQueryable<IMyTable<T>> query = (IQueryable<IMyTable<T>>) _myTableRepository.Where(c => c.Name == a);
if (columnsToSelect == null)
columnsToSelect = (Expression<Func<IMyTable<T>, T>>)(table => (table.GetAll()));
return query.Select(columnsToSelect).ToList();
}
Demo to use
IMyTable<List<object>> xxx = new MyTable() { Surname = "b", Addresss = 1 };
xxx.Name = "a";
IMyTable<List<object>> yyy = new MyTable() { Surname = "d", Addresss = 2 };
yyy.Name = "c";
var repo = new List<IMyTable<List<object>>>() { xxx, yyy }.AsQueryable();
var Test = new Test<List<object>>(repo);
var generic = Test.GetSelection<List<object>>("c");
var specific = Test.GetSelection<List<object>>("c",
(Expression<Func<IMyTable<List<object>>, List<object>>>)
(x => new List<object>() { x.Name, ((MyTable)x).Addresss }));
var specifc2Columns = specific
.Select(rows => new { Name = rows[0], Address = rows[1] });
where is the demo table
internal class MyTable : IMyTable<List<object>>
{
public string Surname { get; set; }
public int Addresss { get; set; }
string IMyTable<List<object>>.Name{ get; set; }
List<object> IMyTable<List<object>>.GetAll()
{
return new List<object>() { ((IMyTable<List<object>>)this).Name, Surname, Addresss };
}
}
user6996876
source
share