【发布时间】:2012-03-08 01:35:12
【问题描述】:
这是我的代码:
// I STORE THE USER SELECTED VALUES
IList<string> SelectedCity = (from CheckBox loc in panelCity.Controls.OfType<CheckBox>()
where loc.Checked
select loc.InputAttributes["value"]).ToList();
IList<string> SelectedCategories = (from CheckBox strut in panelCategories.Controls.OfType<CheckBox>()
where strut.Checked
select strut.InputAttributes["value"]).ToList();
// I GET ALL RECORDS
IList<Hotel> Hotels = (from Hotel hotel in new Hotels()
orderby hotel.Titolo ascending
select hotel).ToList();
// I FILTER THEM
if (SelectedCity.Count > 0)
Hotels = Hotels.Where(o => o.City != null && SelectedCity.Contains(o.City.UniqueID)).ToList();
if (SelectedCategories.Count > 0)
Hotels = Hotels.Where(o => o.Category != null && SelectedCategories.Contains(o.Category.UniqueID)).ToList();
所以,如您所见,我做了一些查询,存储从用户中选择的值。比,对于每个酒店行,我循环城市和类别的数组。
这个,有很多行,非常昂贵。
您知道其他优化研究的方法吗?
【问题讨论】:
-
您是否尝试过仅在最终结果中使用
ToList()?这样做时不要忘记更改变量的类型。