目的:
我需要循环遍历所有记录,例如:
然后循环。
var records = db.Set<UserAccount>().ToList();
然后循环。
foreach (var record in records)
{
// do something with the record
}
但是它在运行时不能指定类型,因为我需要循环遍历类型,所以不知道例如“UserAccount”。 只有Type / TypeOf?
在此描述的底部,我有一个名为loopAllEntities的方法,我找不到使其工作的方法。
我已经创建了一个DbContext和一些实体。
public class MyEntities : DbContext
{
public DbSet<UserAccount> UserAccounts { get; set;}
public DbSet<UserRole> UserRoles { get; set; }
public DbSet<UserAccountRole> UserAccountRoles { get; set; }
}
定义了一系列的类型来控制输出:
public static List<Type> ModelListSorted()
{
List<Type> modelListSorted = new List<Type>();
modelListSorted.Add(typeof(UserRole));
modelListSorted.Add(typeof(UserAccountRole));
modelListSorted.Add(typeof(UserAccount));
return modelListSorted;
}
下面的问题是我找不到解决方法,让它能够工作 :-(
public static loopAllEntities()
{
List<Type> modelListSorted = ModelHelper.ModelListSorted();
foreach (Type type in modelListSorted)
{
var records = ????? // get a list of records in the current table from type.
foreach (var record in records)
{
// do something with the record
}
}
}