使用反射与Linq进行选择

时间:2019-08-24 17:15:07

标签: c# entity-framework backend

几天前,我提出了一个问题:如何从变量中获取表名,如何插入到数据表中?

这是解决方案:

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();

现在我想做类似的事情,但是这次我想做一个“动态”选择,像这样:

SELECT *
FROM nameOfTable 
where foreign_key = number

我想获取所有符合条件的行,并且y要通过变量传递表名。

0 个答案:

没有答案