0

I've never use reflection and I need to query the database and pass the table name dynamically. All tables that I am querying have the same signature(the same columns). I tried to follow the example from Dynamically set the table name in LINQ query but I am stuck now. I tried to call the 'Example' method, but I cannot call it like that. Thank you in advance for the help. I am completely clueless with reflection, if there is a better way of achieving my goal please point me to the right direction.

public void Example(IQueryable<IFileTable> dataSource)
{
    var data = (from q in dataSource
            select new 
            {
                ID = q.Id,
                FileName = q.FileName
            }).ToList();

     Console.Write(data);
}

public static void GetData()
{
    Example(DataConnection.Context.Test1.AsQueryable());
}
  

public interface IFileTable
{
    int Id { get; set; }
    string FileName { get; set; }
}

public partial class Test1: IFileTable
{
    public int Id { get; set; }
    public string FileName { get; set; }
}

public partial class Test2: IFileTable
{
    public int Id { get; set; }
    public string FileName { get; set; }
}
Lio Liov
  • 3
  • 2
  • 2
    You said the code errors out - please could you edit the question to include the full error you get? – Tim Dec 09 '20 at 10:24
  • Welcome to SO please read [ask] and take the [tour] – Ackdari Dec 09 '20 at 10:25
  • Sorry I changed the title. The code doesn't error out. I just cannot call the method like that. – Lio Liov Dec 09 '20 at 10:28
  • Have you checked these? [1](https://stackoverflow.com/questions/47910340/how-to-get-dbset-from-entity-name-in-ef-core-net-core-2-0), [2](https://stackoverflow.com/questions/48041821/dynamically-access-table-in-ef-core-2-0), [3](https://stackoverflow.com/questions/28099435/dynamic-table-name-in-linq/28101268), etc. – Peter Csala Dec 09 '20 at 11:38

0 Answers0