這是我的代碼:你知道另一種方式來做這個「大規模」linq過濾嗎?
// 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()'只在最後結果如何?不要在這樣做的時候改變變量的類型。 – svick 2012-03-07 14:00:52