EF得到从类型运行记录表 [英] EF get list of records in runtime from Type

查看:83
本文介绍了EF得到从类型运行记录表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目标:
我需要循环中的所有记录,如:

  VAR记录= db.Set< UserAccount> ().ToList(); 



然后循环

 的foreach(在记录VAR记录)
{
//做记录
的东西}

但它不特定类型在运行时,因为我遍历类型并为此不知道如UserAccount。
只有类型/ TypeOf运算?



在此说明的底部我有一个方法 loopAllEntities ,我不能找到一种方法,工作



我已经创建了一个的DbContext一些实体。

 公开类MyEntities:的DbContext 
{
公共DbSet< UserAccount> UserAccounts {搞定;设置;}
公共DbSet<&UserRole的GT; {的UserRole获取;组; }
公共DbSet< UserAccountRole> UserAccountRoles {搞定;组; }
}



自定义类型的列表来控制输出:

 公共静态列表<类型> ModelListSorted()
{
名单,LT;类型> modelListSorted =新的List<类型和GT;();
modelListSorted.Add(typeof运算(的UserRole));
modelListSorted.Add(typeof运算(UserAccountRole));
modelListSorted.Add(typeof运算(UserAccount));
返回modelListSorted;
}

问题是下面,我不能找到一种方法,使其工作: - (

 公共静态loopAllEntities()
{
名单<类型> modelListSorted = ModelHelper.ModelListSorted() ;

的foreach(在modelListSorted型型)
{
VAR记录= ????? //获取从类型$ b $当前表中的记录列表。 b的foreach(在记录VAR记录)
{
//做记录
的东西}
}
}


解决方案

它看起来像你几乎在那里,你应该能够做到像下面这样:

 公共静态无效loopAllEntities(的DbContext的DbContext)
{
名单<类型> modelListSorted = ModelHelper.ModelListSorted();

的foreach(在modelListSorted型型)
{
VAR记录= dbContext.Set(类型);
//做的这里设置
}
}



的东西

这将让你一个非通用组的工作。这取决于从那里你想做的事,因为没有类型与本设置它可以得到一个有点棘手什么,也许转换为一个基本类型或接口?



编辑:我没有足够的信誉发表评论,但Mayoors解决方案会得到你想要的东西,而不使用非泛型类型,并得到整个集合了前面


Purpose: I need to loop all records like:

var records = db.Set<UserAccount>().ToList();

Then loop

foreach (var record in records)
{
    // do something with the record 
}

But it has to not type specific in runtime, as I am to loop through types and therefor do not know example "UserAccount". Only the Type/TypeOf?

In the bottom of this description I have a method loopAllEntities that I cannot find a way to work

I have created an DbContext with some entities.

public class MyEntities : DbContext
{
    public DbSet<UserAccount> UserAccounts { get; set;}
    public DbSet<UserRole> UserRoles { get; set; }
    public DbSet<UserAccountRole> UserAccountRoles { get; set; }
}

Defined a list of Type to control the output:

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;
}

The problem is below where I cannot find a way to make it Work :-(

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
        } 
    }
}

解决方案

It looks like you are almost there, you should be able to do something like the following:

    public static void loopAllEntities(DbContext dbContext)
    {
        List<Type> modelListSorted = ModelHelper.ModelListSorted();

        foreach (Type type in modelListSorted) 
        {
            var records = dbContext.Set(type);
            // do something with the set here
        }
    }

This will get you a non generic set to work with. It depends from there what you want to do as there is no type to this set it can get a bit tricky, maybe cast to a base type or interface to work with?

Edit: I don't have enough reputation to comment, but Mayoors solution would get you what you want without using a non-generic type and get the whole collection up front.

这篇关于EF得到从类型运行记录表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆