[英]C# using Where (Linq) to filter a list with four condition
我有一個列表從數據庫中獲取(使用數據集):
IEnumerable<DataSet.spGetDataRow> MyList = new DataSetTableAdapters.spGetDataTableAdapter().GetData(Date1, Date2, PID, RID).ToList();
我有4個復選框
<asp:CheckBox ID="CheckBox1" Text="1" runat="server" />
<asp:CheckBox ID="CheckBox2" Text="2" runat="server" />
<asp:CheckBox ID="CheckBox3" Text="3" runat="server" />
<asp:CheckBox ID="CheckBox4" Text="4" runat="server" />
我需要過濾“ MyList”
我做了以下事情:
if (CheckBox1.Checked && !CheckBox2.Checked && !CheckBox3.Checked && !CheckBox4.Checked)
{
MyList = MyList.Where(a => a.Avg < 2);
}
else if (CheckBox2.Checked && !CheckBox1.Checked && !CheckBox3.Checked && !CheckBox4.Checked)
{
MyList = MyList.Where(a => a.Avg >= 2 && a.Avg < 3);
}
else if (CheckBox3.Checked && !CheckBox2.Checked && !CheckBox1.Checked && !CheckBox4.Checked)
{
MyList = MyList.Where(a => a.Avg >= 3 && a.Avg < 6);
}
else if (CheckBox4.Checked && !CheckBox2.Checked && !CheckBox3.Checked && !CheckBox1.Checked)
{
MyList = MyList.Where(a => a.Avg >= 6);
}
如果我一次只需要選中一個復選框,那么這項工作非常有用,但是如果我一次只需要選中2或3,該怎么辦。
如果條件不是最好的解決方案,那么創建那么多,如果可能,我需要通過linq來完成
我正在做的是創建一個新的MyList,並將過濾后的MyList“連接”到其中,但是效果並不理想。
先感謝您。
反之亦然。 測試每個復選框。 如果未選中,請刪除此復選框代表的條目。 或更好地采取那些沒有代表。 因此,如果未選中CheckBox1,則采用那些不小於2的條目,這意味着它們大於等於2。
if (!CheckBox1.Checked)
{
MyList = MyList.Where(a => a.Avg >= 2);
}
if (!CheckBox2.Checked)
{
MyList = MyList.Where(a => a.Avg < 2 || a.Avg >= 3);
}
if (!CheckBox3.Checked)
{
MyList = MyList.Where(a => a.Avg < 3 || a.Avg >= 6);
}
if (!CheckBox4.Checked)
{
MyList = MyList.Where(a => a.Avg < 6);
}
在下一步中,您應該預先計算平均值,這樣就不會多次調用此函數。 例如,建立元組(行,平均值),然后過濾平均值,並通過從元組中提取行來取回行。
您的問題使我很想用詞典找到解決方案。 這會將您的代碼留給像這樣的簡單兩行(同樣需要一個簡單的函數)
int index = CreateIndexFromCheckboxSelected();
var result = MyList.Where(x => dict[index].Invoke(x));
要實現此解決方案,您需要使用數字鍵和Func作為值來構建一個Dictionary。
數字鍵是關於布爾邏輯的各個復選框的字節總和,其中,checkBox1表示位置1的位,checkbox2表示位置2的位,依此類推。
Func部分是應用於您的sqGetDataRow
元素列表的where子句所需的lambda表達式
您需要在此字典中有16個條目,每個條目對應4個復選框的可能組合,每個條目都包含lambda表達式,該表達式將在where子句中使用以提取所需的spGetDataRow元素。
Dictionary<int, Func<spGetDataRow,bool>> dict = new Dictionary<int, Func<spGetDataRow, bool>>()
{
{0, (spGetDataRow a) => true}, // No checkboxes checked return all
{1, (spGetDataRow a) => a.Avg < 2}, // Only checkbox1 checked
{2, (spGetDataRow a) => a.Avg >= 2 && a.Avg < 3}, // Only checkbox2 checked
{3, (spGetDataRow a) => a.Avg ????}, // CheckBox1 AND Checkbox2 checked
{4, (spGetDataRow a) => a.Avg >= 3 && a.Avg < 6}, // CheckBox3 checked
{5, (spGetDataRow a) => a.Avg ????}, // CheckBox3 + ChecBox1 checked
{6, (spGetDataRow a) => a.Avg >= 2 && a.Avg < 6}, // CheckBox3 + CheckBox2 checked
{7, (spGetDataRow a) => a.Avg ????}, // CheckBox3 + CheckBox2 + CheckBox1 checked
{8, (spGetDataRow a) => a.Avg >= 6}, // CheckBox4 checked
{9, (spGetDataRow a) => a.Avg ????}, // CheckBox4 + CheckBox1 checked
{10, (spGetDataRow a) => a.Avg ????}, // CheckBox4 + CheckBox2 checked
{11, (spGetDataRow a) => a.Avg ????}, // CheckBox4 + CheckBox2 + CheckBox1 checked
{12, (spGetDataRow a) => a.Avg ????}, // CheckBox4 + CheckBox3 checked
{13, (spGetDataRow a) => a.Avg ????}, // CheckBox4 + CheckBox3 + CheckBox1 checked
{14, (spGetDataRow a) => a.Avg ????}, // CheckBox4 + CheckBox3 + CheckBox2 checked
{15, (spGetDataRow a) => true} // All checkboxes selected return all
};
上面的示例僅包含我可以從您的問題中提取的信息的正確lambda,但是如您所見,將占位符lambda替換為各種復選框組合所需的正確lambda只是一個問題。
為了完成該示例,僅遺漏了一部分,但這是微不足道的
public int CreateIndexFromCheckboxSelected()
{
int chk1 = (CheckBox1.Checked ? 1 : 0);
int chk2 = (CheckBox2.Checked ? 2 : 0);
int chk3 = (CheckBox3.Checked ? 4 : 0);
int chk4 = (CheckBox4.Checked ? 8 : 0);
return (chk1 + chk2 + chk3 + chk4);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.