ASP.NETWebフォームを使用しています。属性に基づいて商品をフィルタリングする機能を作成しています。
今のやり方は時間がかかりすぎています。より良い、より迅速な方法が必要です。
私の現在の方法は次のとおりです。属性名を表示するGridViewがあります。CheckBoxListは、属性値を含むItemTemplateにネストされています。CheckBoxListにはautopostback="true"があります。selectedindexchangedイベントごとに、チェックボックスをループし、チェックされた状態に基づいてフィルタリングします。次に、フィルタリングされた結果とチェックボックスの値を比較し、フィルタリングされた結果に含まれていないチェックボックスを無効にします。
これを行うコードは次のとおりです。
protected void cblAttr_SelectedIndexChanged(object sender, EventArgs e)
{
var filteredResults = ProductAttribute.Get(Convert.ToInt32(Request.QueryString["idsite"]));
var selectedAttributes = filteredResults;
foreach (GridViewRow row in gridAttrSet.Rows)
{
foreach (ListItem item in ((CheckBoxList)row.FindControl("cblAttr")).Items)
{
if (item.Selected == true)
{
// filter
selectedAttributes = selectedAttributes.Where(a => a.idAttr == Convert.ToInt32(item.Value)).ToList();
}
}
}
// this will now contain
filteredResults = (from c in filteredResults
join o in selectedAttributes
on c.idProduct equals o.idProduct
select new ProductAttribute
{
idProduct = c.idProduct,
idAttrSet = c.idAttrSet,
idAttr = c.idAttr,
}).ToList();
foreach (GridViewRow row in gridAttrSet.Rows)
{
if (row.RowIndex > ((GridViewRow)((CheckBoxList)sender).NamingContainer).RowIndex)
{
foreach (ListItem item in ((CheckBoxList)row.FindControl("cblAttr")).Items)
{
// disable if filtered out
item.Enabled = !(filteredResults.Where(a => a.idAttr == Convert.ToInt32(item.Value)).ToList().Count == 0);
}
}
}
}
ご覧のとおり、多くのループが発生しており、何千ものレコードが存在する可能性があります。
これを行うためのより速い方法は何でしょうか?任意のアイデアをいただければ幸いです。
ありがとうございました!