实现包含以下内容:
List<id>
从表中获取。该表包含应为其生成报告的记录。如果该表没有记录,那么将不会生成报告。代码如下所示:
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;}
}
比您可以通过多个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] 删除。
我来说两句