2013-08-07 55 views
0

我有這樣的情況。我有列表裏面我持有一個類的對象(這個對象有6個屬性「名稱」,「類型」,「索引」,「價值」,「狀態」,「參數」)。後來我用gridView綁定它。 現在我想能夠爲每個屬性製作過濾器。我希望能夠例如插入到文本框「名稱」:約翰,我希望gridView只顯示我有約翰的行。c#類對象過濾

第二件事是我想能夠混合過濾器,所以例如將「Name」設置爲:'John'和'Index'爲:5,並用「Index」顯示「John」: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財產Line

對不起,如果我真的弄亂!

回答

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(); 
0

使用LINQ過濾器

yourlist.Select(x=>x.Name == "John"); 
0

您可以定義一些「NoFilter」值。例如指數可能爲-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);