gpt4 book ai didi

c# - LINQ:动态 Where 子句切换大小写组合

转载 作者:行者123 更新时间:2023-11-30 19:53:11 24 4
gpt4 key购买 nike

我有一个绑定(bind)到 angridview 的列表,我希望能够“即时”创建过滤器 Where 子句,而过滤器选项的组合由用户控制。

按模型 bool 属性过滤原始列表并允许切换每个过滤条件的最佳方法是什么?

我知道 filterdList 不是必需的,但我已经看到的所有其他解决方案都不允许切换 Where 子句的条件。

public partial class Form1 : Form
{
List<dummy> Origlist = new List<dummy> {
new dummy { pk = 1 , istype1 = true, istype2 = false, istype3=false, istype4=false },
new dummy { pk = 2 , istype1 = true, istype2 = false, istype3=false, istype4=false },
new dummy { pk = 3 , istype1 = false, istype2 = true, istype3=false, istype4=false },
new dummy { pk = 4 , istype1 = false, istype2 = true, istype3=false, istype4=false },
new dummy { pk = 5 , istype1 = false, istype2 = false, istype3=true, istype4=false },
new dummy { pk = 6 , istype1 = false, istype2 = false, istype3=true, istype4=false },
new dummy { pk = 7 , istype1 = false, istype2 = false, istype3=false, istype4=true },
new dummy { pk = 8 , istype1 = false, istype2 = false, istype3=false, istype4=true },
new dummy { pk = 9 , istype1 = false, istype2 = false, istype3=true, istype4=false },
new dummy { pk = 10 , istype1 = false, istype2 = true, istype3=false, istype4=false },
new dummy { pk = 11 , istype1 = false, istype2 = false, istype3=false, istype4=false }
};
List<dummy> filteredList = new List<dummy>();
public Form1()
{
InitializeComponent();
}

private void Bind()
{
dataGridView1.DataSource = null;
dataGridView1.DataSource = filteredList;
}


private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
if (checkBox1.Checked)
{
filteredList.AddRange(Origlist.Where(a => a.istype1 == true).ToList());
}
else
{
filteredList.RemoveAll(a => a.istype1 == true);
}
Bind();
}



private void checkBox2_CheckedChanged(object sender, EventArgs e)
{
if (checkBox2.Checked)
{
filteredList.AddRange(Origlist.Where(a => a.istype2 == true).ToList());
}
else
{
filteredList.RemoveAll(a => a.istype2 == true);
}
Bind();
}

private void checkBox3_CheckedChanged(object sender, EventArgs e)
{
if (checkBox3.Checked)
{
filteredList.AddRange(Origlist.Where(a => a.istype3 == true).ToList());
}
else
{
filteredList.RemoveAll(a => a.istype3 == true);
}
Bind();
}

private void checkBox4_CheckedChanged(object sender, EventArgs e)
{
if (checkBox4.Checked)
{
filteredList.AddRange(Origlist.Where(a => a.istype4 == true).ToList());
}
else
{
filteredList.RemoveAll(a => a.istype4 == true);
}
Bind();
}

}

simulation

最佳答案

尝试这样的事情(未经测试):

public partial class Form1 : Form
{
List<dummy> Origlist = new List<dummy> {
new dummy { pk = 1 , istype1 = true, istype2 = false, istype3=false, istype4=false },
new dummy { pk = 2 , istype1 = true, istype2 = false, istype3=false, istype4=false },
new dummy { pk = 3 , istype1 = false, istype2 = true, istype3=false, istype4=false },
new dummy { pk = 4 , istype1 = false, istype2 = true, istype3=false, istype4=false },
new dummy { pk = 5 , istype1 = false, istype2 = false, istype3=true, istype4=false },
new dummy { pk = 6 , istype1 = false, istype2 = false, istype3=true, istype4=false },
new dummy { pk = 7 , istype1 = false, istype2 = false, istype3=false, istype4=true },
new dummy { pk = 8 , istype1 = false, istype2 = false, istype3=false, istype4=true },
new dummy { pk = 9 , istype1 = false, istype2 = false, istype3=true, istype4=false },
new dummy { pk = 10 , istype1 = false, istype2 = true, istype3=false, istype4=false },
new dummy { pk = 11 , istype1 = false, istype2 = false, istype3=false, istype4=false }
};

Options options = new Options();

private class Options {
public bool istype1 { get; set; }
public bool istype2 { get; set; }
public bool istype3 { get; set; }
public bool istype4 { get; set; }
}

public Form1()
{
InitializeComponent();
}

private void Bind()
{
dataGridView1.DataSource = null;
dataGridView1.DataSource = OrigList.Where(a =>
(options.istype1 && a.istype1) ||
(options.istype2 && a.istype2) ||
(options.istype3 && a.istype3) ||
(options.istype4 && a.istype4)
).ToList();
}


private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
options.istype1 = checkBox1.checked;
Bind();
}



private void checkBox2_CheckedChanged(object sender, EventArgs e)
{
options.istype2 = checkBox2.checked;
Bind();
}

private void checkBox3_CheckedChanged(object sender, EventArgs e)
{
options.istype3 = checkBox3.checked;
Bind();
}

private void checkBox4_CheckedChanged(object sender, EventArgs e)
{
options.istype4 = checkBox4.checked;
Bind();
}

}

还有其他方法可以减少重复,但这只是一个开始。

关于c# - LINQ:动态 Where 子句切换大小写组合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50466693/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com