0

我有这样的情况。我有一个列表,其中我持有一个类的对象(这个对象有 6 个属性“名称”、“类型”、“索引”、“值”、“状态”、“参数”)。后来我将它与gridView绑定。现在我希望能够为每个属性制作一个过滤器。例如,我希望能够插入文本框“姓名”:John,并且我希望 gridView 仅显示我有 John 的行。

第二件事是我希望能够混合过滤器,例如将“名称”设置为:“约翰”,将“索引”设置为:5,并显示“约翰”和“索引”: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);
}

编辑:

在我的情况下并不是那么简单(我认为......),因为我有主类,而我在上面显示了这个。后来我从 class 调用方法 HadleRegex Correction。下面我将展示它的外观:

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类的属性中Line

如果我真的搞砸了,我很抱歉!

4

3 回答 3

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();
于 2013-08-07T06:36:39.793 回答
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);
于 2013-08-07T06:44:58.283 回答
0

使用 linq 到文件管理器

yourlist.Select(x=>x.Name == "John");
于 2013-08-07T06:37:13.703 回答