如何使用Entity Framework 6从10多个表中获取记录并显示其中的报告?

比杰·亚达夫(Bijay Yadav)

实现包含以下内容:

  1. List<id>从表中获取该表包含应为其生成报告的记录。如果该表没有记录,那么将不会生成报告。
  2. 其余细节在代码中内联。

代码如下所示:

List<int> totalValidRecords = ; //This comes from a table on the basic of which the report will be generated.

foreach(int id in totalValidRecords)
{
   List<Region> regions= //gets list of record from Region table.

   foreach(Region region in regions)
   {
      List<Country> countries= //gets list of countries from country table based on region.

      foreach(Country country in counties)
      {
        List<State> states = //gets list of states from State table based on country.

        foreach(State state in states)
        {
           List<District> states = //gets list of districts from District table based on state.

           //Other logic which computes and access other rest of dependent tables data.
        }
      }
   }

}

该代码可以正常工作,但是只需要花费大约20秒钟的时间即可读取几条记录(大约20条记录)。

生成报告的这种延迟可能是由于发生了许多Database调用,但是我不能忽略那些调用,因为需要这些来生成reports

如果需要进一步澄清问题,请告诉我。

斯拉瓦·乌特西诺夫(Slava Utesinov)

假设您的模型如下所示:

public class Base
{
    public int Id {get;set;}
}

public class Region : Base
{
}

public class Country : Base
{
    public Region Region {get;set;}
    public int RegionId {get;set;}
}

public class State : Base
{
    public Country Country {get;set;}
    public int CountryId {get;set;}
}

public class District : Base 
{
    public State State {get;set;}
    public int StateId {get;set;}
}

比您可以通过多个s编写单个查询join

var answer = (from region in db.Regions.Where(x => totalValidRecords.Contains(x.Id))
              join country in db.Country on region.Id equals country.RegionId 
              join state in db.States on country.Id equals state.CountryId 
              join district in db.Districts on state.Id equals district.StateId 
              select new 
              {
                  regionId = region.Id,
                  countryId = country.Id,
                  stateId = state.Id,
                  districtId = district.Id
                  //other fields
              }).ToList();

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何使用Entity Framework 7记录查询?

使用Entity Framework Core获取多个表

如何使用存储过程在Entity Framework中获取身份?

如何使用Entity Framework + OData发布多个相关实体?

如何使用Entity Framework和MVC显示导航属性的值?

如何使用.include查询强制外部联接Entity Framework 6

如何使用Entity Framework 6创建内存DbContext?

如何使Entity Framework 6在CSDL中使用SQL STUFF函数?

如何使用Entity Framework 6调用SQL Server过程

如何使用Entity Framework Core或Dapper批量更新表的列?

如何使用 FromSqlRaw 在 Entity Framework Core 中返回 int 列表?

如何使用FirstAsync在Entity Framework Core中处理null?

如何首先使用代码在Entity Framework 6.2中创建索引

如何使用Entity Framework从SQL表中获取两个列值的总和?

使用多个列在Entity Framework中查找SQL表中的所有重复记录

如何使用 ASP.NET Core、Identity Framework 和 Entity Framework 显示特定用户创建的内容

如何使用Entity Framework ASP.Net MVC 5删除多个记录?

如何在不使用 SQL 的情况下使用 Entity Framework 有效删除表中的所有记录?

如何使用Entity Framework 6在具有多个外键的实体上应用级联删除?

如何使用Entity Framework 6更新具有唯一列的表上的条目

如何使用Entity Framework 6在运行时创建数据库和表?

如何使用Entity Framework描述同一表格中记录之间的关系

如何使用 Entity Framework C# 获取与特定表相关的表名

如何使用LINQ从表中选择存在于联结表中的元素-Entity Framework

如何在ASP.NET MVC中使用Entity Framework将记录插入具有外键的表中

使用Entity Framework数据模型从表中获取多个数据

如何使用C#中的Entity Framework从存储过程中获取输出结果?

使用Entity FrameWork 7更新记录

如何使用Entity Framework 6从存储过程中检索输出参数