Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/database/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#使用Where(Linq)过滤具有四个条件的列表_C#_Asp.net_Linq_Dataset_Ienumerable - Fatal编程技术网

C#使用Where(Linq)过滤具有四个条件的列表

C#使用Where(Linq)过滤具有四个条件的列表,c#,asp.net,linq,dataset,ienumerable,C#,Asp.net,Linq,Dataset,Ienumerable,我有一个从数据库获得的列表(使用数据集): IEnumerable MyList=新的DataSetTableAdapters.spGetDataTableAdapter().GetData(Date1、Date2、PID、RID).ToList(); 我有4个复选框 <asp:CheckBox ID="CheckBox1" Text="1" runat="server" /> <asp:CheckBox ID="CheckBox2" Text="2" runat="serv

我有一个从数据库获得的列表(使用数据集):

IEnumerable MyList=新的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);
}
if(CheckBox1.Checked&&!CheckBox2.Checked&&!CheckBox3.Checked&&!CheckBox4.Checked)
{
MyList=MyList.其中(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,其中(a=>a.Avg>=6);
}
如果我一次只需要勾选一个复选框,这很好,但是如果我想一次勾选2或3,该怎么办呢

创建这么多if条件不是最好的解决方案,如果可能的话,我需要通过linq来实现

我所做的是创建一个新的MyList,并将过滤后的MyList“concat”到其中,但结果并不理想


先谢谢你

反之亦然。测试每个复选框。如果未选中该复选框,请删除该复选框表示的条目。或者最好拿那些没有代表的。因此,如果复选框1未选中,则取那些未选中的条目

if(!CheckBox1.Checked)
{
MyList=MyList,其中(a=>a.Avg>=2);
}
如果(!CheckBox2.选中)
{
MyList=MyList.其中(a=>a.Avg<2 | | a.Avg>=3);
}
如果(!CheckBox3.选中)
{
MyList=MyList.其中(a=>a.Avg<3 | | a.Avg>=6);
}
如果(!CheckBox4.选中)
{
MyList=MyList.其中(a=>a.Avg<6);
}

在下一步中,您应该预先计算平均值,这样该函数就不会被调用太多次。例如,构建元组(行,平均值),然后过滤平均值,并通过从元组中提取行来获取行

你的问题激起了我的兴趣,我想用字典找到一个解决办法。这将使您的代码只剩下这样简单的两行代码(同时还需要一个简单的函数)

要实现此解决方案,您需要使用数字键和Func作为值构建字典

数字键是关于布尔逻辑的各种复选框的字节和,其中复选框1表示位置1处的位,复选框2表示位置2处的位,依此类推

Func部分是应用于
sqGetDataRow
元素列表的where子句所需的lambda表达式

此字典中需要16个条目,4个复选框的可能组合各有一个条目,每个条目包含要在where子句中使用的lambda表达式,以提取所需的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
};

你能把你想要的要求贴在你的过滤器上吗。例如,
Checkbox1
是唯一选中的复选框,那么
a.AvgWell如果(比如)选中复选框2和3,您希望效果如何?如果什么都没有选中怎么办?@JonSkeet:如果什么都没有选中,则显示全部,如果全部选中也显示all@MohamadY.Dbouk但是当2和3被选中时,显示的是什么?@Vladimirs:如果2和3被选中:我的where条件如下:MyList.where(a=>a.Avg>=2&&a.Avg<6);添加“| |”修复它。谢谢
    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));
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
};
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);
}