0

A few days ago I made a question: how could I made a insert into datatable taking the table name from a variable.

This was the solution:

var nameOfTable = "here I saved the name of table where i want to insert my information"

var type = ((IQueryable)db.GetType().GetProperty(nameOfTable).GetValue(db)).ElementType;
var dbSet = db.Set(type);
dbSet.AddRange(informationIWantToInsert);

db.SaveChanges();

Now I want to make something similar but this time I want to make a "dynamic" select, like this:

SELECT *
FROM nameOfTable 
where foreign_key = number

I want to get all rows that meet the condition and y want to pass the name of table by variable.

4
  • DataTable results = table.AsEnumberable().Where(x => x.name = "abc").CopyToDataTable();
    – jdweng
    Aug 24, 2019 at 18:08
  • sorry it was my mistake. the variable table is a json with information that i insert into my data base. The important here is the variables type and dbSet Aug 24, 2019 at 18:35
  • Your SQL used table. So isn't the database then db.table.AsEnumerable()
    – jdweng
    Aug 24, 2019 at 22:03
  • Are you using reflection only to get a dynamic table name? I hope you know we can fire raw queries using EF. learn.microsoft.com/en-us/ef/ef6/querying/raw-sql Aug 25, 2019 at 7:05

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.