我正在从单个 View 中查询以检索指标列表。可以从该单一 View 中检索指标的所有属性。
代码如下:
data = new DataContext();
var core = from item in data.Items
where countryIDs.Contains(item.CountryId)
&& indicatorIDs.Contains(item.IndicatorId)
orderby item.Indicator
select item;
var x = from item in core.Distinct()
group item by new { item.IndicatorId, item.Indicator }
into indicator
select new
{
IndicatorID = indicator.Key.IndicatorId,
IndicatorDescription = indicator.Key.Indicator,
Genders = from g in core
where g.Gender != null
&& g.IndicatorId == indicator.Key.IndicatorId
select new Gender
{
GenderID = g.GenderId,
GenderDescription = g.Gender
},
HasGender = (from g in core
where g.Gender != null
&& g.IndicatorId == indicator.Key.IndicatorId
select g.GenderId).Count() > 0,
AreaTypes = from rat in core
where rat.IndicatorId == indicator.Key.IndicatorId
&& rat.AreaType != null
select new AreaType
{
AreaTypeId = rat.AreaTypeId,
AreaDescription = rat.AreaType
},
HasAreaType = (from rat in core
where rat.IndicatorId == indicator.Key.IndicatorId
&& rat.AreaType != null
select rat.AreaTypeId).Count() > 0,
Sectors = from s in core
where s.IndicatorId == indicator.Key.IndicatorId
&& s.Sector != null
select new Sector
{
SectorID = s.SectorId,
Title = s.Sector
},
HasSector = (from s in core
where s.IndicatorId == indicator.Key.IndicatorId
&& s.Sector != null
select s.SectorId).Count() > 0
};
List<Indicator> indicators = new List<Indicator>();
Indicator i = new Indicator();
foreach (var item in x)
{
i = new Indicator()
{
IndicatorID = item.IndicatorID,
IndicatorDescription = item.IndicatorDescription,
Genders = item.Genders.ToList(),
AreaTypes = item.AreaTypes.ToList(),
Sectors = item.Sectors.ToList(),
HasGender = item.HasGender,
HasAreaType = item.HasAreaType,
HasSector = item.HasSector
};
indicators.Add(i);
}
return indicators;
当它到达 foreach 循环时变慢,当 x 被转换时。 有什么方法可以使此查询更快地转换为列表吗?谢谢。
请您参考如下方法:
看起来你在做很多不必要的嵌套查询。
您的核心 查询在返回项目之前进行一些相对昂贵的过滤和排序。最好只执行此查询一次。
但是,您正在对该查询执行六个不必要的连接。
例如,您的查询 Genders 正在重新查询 core 并且只保留具有相同 IndicatorId 且您已经分组的项目!如果我可以假设 item.Indicator 与 item.IndicatorId 是一对一的,那么您的组 indicator 已经包含这个子集。
您正在以相同的方式查询 AreaTypes 和 Sectors。
现在每个 HasGender、HasAreaType 和 HasSector 都重复上面的查询并强制一个.Count() 对它们中的每一个仅检查值是否大于零。这是一种浪费,因为 .Any() 会以更便宜的方式检查至少一个值。
现在为了测试 core 查询被访问了多少次,我创建了这个测试代码:
var countryIDs = Enumerable.Range(0, 100).ToArray();
var indicatorIDs = Enumerable.Range(0, 100).ToArray();
data.Items.AddRange(
Enumerable
.Range(0, 100)
.Select(n =>
new Item()
{
CountryId = n,
IndicatorId = n,
Indicator = "Indicator",
GenderId = n,
Gender = "Gender",
AreaTypeId = n,
AreaType = "Area",
SectorId = n,
Sector = "Sector",
}));
我将 core 修改为如下所示:
var counter = 0;
var core =
(from item in data.Items
where countryIDs.Contains(item.CountryId)
&& indicatorIDs.Contains(item.IndicatorId)
orderby item.Indicator
select item).Do(_ => counter++);
Do 运算符来自 Reactive Extensions System.Interactive 程序集。
运行你的代码我得到以下结果:
counter == 60100
因为我在集合中放入了 100 个项目,这告诉我您的查询正在调用 core 的新执行 601 次!
这可以很容易地更改为执行一次 core。
首先我修改了 core 看起来像这样:
var query =
from item in data.Items
where countryIDs.Contains(item.CountryId)
&& indicatorIDs.Contains(item.IndicatorId)
orderby item.Indicator
select item;
var core = query.ToArray();
.ToArray() 将查询结果存入内存。
然后将 x 查询修改为如下所示:
var x =
from item in core.Distinct()
group item by new
{
item.IndicatorId,
item.Indicator
} into indicator
let Genders = (
from g in indicator
where g.Gender != null
select new Gender
{
GenderID = g.GenderId,
GenderDescription = g.Gender,
}).ToList()
let AreaTypes = (
from rat in indicator
where rat.AreaType != null
select new AreaType
{
AreaTypeId = rat.AreaTypeId,
AreaDescription = rat.AreaType,
}).ToList()
let Sectors = (
from s in indicator
where s.Sector != null
select new Sector
{
SectorID = s.SectorId,
Title = s.Sector,
}).ToList()
select new Indicator()
{
IndicatorID = indicator.Key.IndicatorId,
IndicatorDescription = indicator.Key.Indicator,
Genders = Genders,
AreaTypes = AreaTypes,
Sectors = Sectors,
HasGender = Genders.Any(),
HasAreaType = AreaTypes.Any(),
HasSector = Sectors.Any(),
};
请注意,我只计算了每个 Genders、AreaTypes 和 Sectors 一次,并将每个创建为一个列表。这允许我更改 x 以立即生成 Indicator 的实例。
现在 indicators 列表的最终创建很简单:
var indicators = x.ToList();
当我在此方法上使用示例数据时,我的结果是这样的:
counter == 100
这意味着这个查询只命中一次原始的核心查询!
然后,当我将原始示例数据增加到 1,000 个项目时,我检查了嵌套的行为方式 - 我使用新代码获得了一次命中,使用原始代码获得了 6,001 次命中 - 而且速度慢了很多。
请记住,LINQ 是延迟计算的,因此执行不会发生在您定义查询的地方,而是发生在执行它的地方。
因此这里的建议是,在内存允许的情况下,您应该尽快执行查询以将数据存入内存,然后只执行一次计算。
