インジケーターのリストを取得するために、単一のビューからクエリを実行しています。インジケーターのすべてのプロパティは、その単一のビューから取得できます。
コードは次のとおりです。
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;
xが変換されると、foreachループに到達すると速度が低下します。このクエリをリストにすばやく変換する方法はありますか?ありがとうございました。