簡體   English   中英

如何使用 Entity Framework 6 從 10 個以上的表中獲取記錄並顯示其中的報告?

[英]How to fetch records from more than 10 tables and shows the report out of them using Entity Framework 6?

實現包含以下內容:

  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需要這些調用。

如果對這些問題需要更多說明,請告訴我。

假設您的模型如下所示:

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

您可以通過多個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();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM