我有一种情况。我有一个列表,其中我持有一个类的对象(此对象有6个属性“名称”,“类型”,“索引”,“值”,“状态”,“参数”)。后来我用gridView绑定它。 现在我希望能够为每个属性创建一个过滤器。我希望能够例如插入文本框“Name”:John,我希望gridView只显示我拥有John的行。
第二件事是我希望能够混合过滤器,所以例如将“Name”设置为:'John'和“Index”为:5,并将“John”显示为“Index”:5。
我该怎么做?
现在我只有将所有内容插入列表的功能。此对象存储在WholeLine类的Middle属性中。
Correction method = new Correction();
while ((line = sr.ReadLine()) != null)
{
string[] _columns = line.Split(",".ToCharArray());
object returnValue;
MyColumns mc = new MyColumns();
mc.Time = _columns[0];
mc.Information = _columns[1];
mc.Description = _columns[2];
if (String.IsNullOrEmpty(mc.Information) )
{ continue; }
else
{
returnValue = method.HandleRegex(mc.Information);
}
Line main = new Line();
main.LeftColumn = mc.Time;
main.Middle= returnValue;
main.RightColumn = mc.Description;
list3.Add(main);
}
编辑:
在我的情况下(我认为......)并不是那么简单,因为我有上课所在的主要课程。后来我从类Correction
调用方法HadleRegex。贝娄我将展示它的样子:
class Correction
{
private MoreInfor _MoreInfor = new MoreInfor();
public MoreInfor MoreInfor { get { return _ID; } }
Correction sk = new Correction();
Match matches = Regex.Match(newStr, @"\b(?:complete\b)", RegexOptions.IgnoreCase);
if (matches.Success)
{
string[] lines = newStr.Split(Environment.NewLine.ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
Regex regex1 = new Regex(@"^(?:(?<C0>Command) (?:answer|sent) from (?<C1>\S+) to (?<C2>\S+) (?<C3>.+))$");
var matches1 = lines.Select(line => regex1.Match(line));
foreach (var match in matches1)
{
sk._MoreInfor.Name= match.Groups["C1"].ToString();
sk._MoreInfor.Type = match.Groups["C2"].ToString();
sk._MoreInfor.Value = match.Groups["C3"].ToString();
sk._MoreInfor.Index = match.Groups["C4"].ToString();
}
}
return sk;
}
public class MoreInfo
{
public string Name{ get; set; }
public string Type { get; set; }
public string Index { get; set; }
public string Value { get; set; }
public string Status { get; set; }
public string Parameter { get; set; }
}
这个返回的课程Correction
稍后会从我的主课程传递到returnValue
并添加到课程Middle
的{{1}}属性中
如果我真的搞砸了,我很抱歉!
答案 0 :(得分:2)
您可以使用linq - where
var result = list.Where(x => x.Name == textBoxName.Text).ToList();
这假设您的文本框将搜索名称
对于多个过滤器,
list.Where(x => x.Property == "something" && x.Name == textBoxName.Text).ToList();
list.Where(x => result.Where(ix => x.Property == "something").ToList();
答案 1 :(得分:0)
使用linq到文件管理器
yourlist.Select(x=>x.Name == "John");
答案 2 :(得分:0)
您可以定义一些“NoFilter”值。例如,index可以是-1,或Name =“”
public List<person> FilterPersons (List<person> persons,
string name = "",
string type = "",
int index = -1,
int value = -1,
int status = -1,
string parameter = "")
{
return persons
.Where
(
x=>
(name == "" || x.Name == name)
&& (type == "" || x.Type == type)
&& (index == -1 || x.Index == index)
&& (value == -1 || x.Value == value)
&& (status == -1 || x.Status == status)
&& (parameter == "" || x.Parameter == parameter)
)
.Select(x=>x);
}
然后你可以称之为:
filterPersons = FilterPersons(persons: persons,name: "John");
或
filterPersons = FilterPersons(persons: persons,index: 10, status: 5);