-1

I have problem I need to get list from list in linq. But it type of list is unknown on compilation stage.

using(var context = MyDbContext())
{
    var list = (from p in context.Employee select p).ToList()
}

I dont know what just property (change Employee)

I want to do something that

public IList<T> GetAll(string propName)
{
   using (var context = new ModelContext())
   {
      return (from p in context.GetType().GetProperty(propName) select p).ToList();
   }
}
juharr
  • 31,741
  • 4
  • 58
  • 93

1 Answers1

0

The DbContext.Database property provides an API that allows you to perform ADO.NET operations directly. The GetDbConnection method returns a DbConnection object representing the context's underlying connection. From that point, you can revert to the familiar ADO.NET APIs:

public dynamic GetAll(string propName)
{

    using (var context = new SampleContext())
    using (var command = context.Database.GetDbConnection().CreateCommand())
    {
        //Escape propName to prevent SQL injection
        var builder = new SqlCommandBuilder();
        string escapedTableName = builder.QuoteIdentifier(propName);

        command.CommandText = $"SELECT * From {escapedTableName}";
        context.Database.OpenConnection();
        using (var dataReader= command.ExecuteReader())
        {
           var dataTable = new DataTable();
           dataTable.Load(dataReader);
           return dataTable ;
        }
    }
}
Mohammad Niazmand
  • 1,509
  • 9
  • 13