c#通用列表过滤器
本文关键字:过滤器 列表 | 更新日期: 2023-09-27 18:26:47
我有一个对象列表。我的课程是这样的:
class Line
{
public string A { get; set; }
public object B { get; set; }
public string C { get; set; }
}
class More_B
{
public Information Information { get { return _Information; }}
public Description Description { get { return _Description ; }}
private Information _Information = new Information();
private Description _Description = new Description ();
}
class Information
{
public string Name { get; set;}
public string Type { get; set;}
public string Further_Info { get; set;}
}
在最后的主类中,我得到Line
类中的所有数据,如下所示。
More_B info = new More_B();
Line main = new Line();
main.A = "Information about device";
main.B = info;
main.C = "Some more information could be display here";
list3.Add(main);
所以在最后我得到了一个列表,里面有所有的3个字段A
B
C
。我在A
和C
中有我的字符串值,在B
中,当我在调试模式下检查时,我有Information对象,其中有所有字段,如Name
、Type
和Further_Info
。
我后来想根据这些值"Name"Type"answers"Further_Info"来过滤这个列,我尝试过这种方式,但它不起作用。(我将列表绑定到DataView以便能够使用RowFilter)。
view2.RowFilter = "Line like '%" + textBox2.Text + "%'";
我该怎么做才对?
我是C#的新手,所以如果我做错了什么,请原谅我。如果有什么难以理解的地方,请告诉我,我会尝试发布更多的代码来解释我所做的事情。
编辑:当我使用此语句view2.RowFilter = "Line like '%" + textBox2.Text + "%'";
时,我得到错误Cannot perform 'Like' operation on read_display.More_B and System.String.
EDIT2:我还有两个方法可以为我的类创建数据表,然后我这样做:DataTable ListAsDataTable2 = BuildDataTable2<Line>(list3); DataView ListAsDataView2 = ListAsDataTable2.DefaultView; this.dataGridView4.DataSource = view2 = ListAsDataView2;
以这种方式尝试:
public class Line
{
public string A {get;set;}
public More_B B {get;set;}
public string C {get;set;}
}
public class Information
{
public string Name { get; set;}
public string Type { get; set;}
public string Further_Info { get; set;}
}
public class More_B:Information
{
public string CusotmField {get;set;}
}
var B_Object = new More_B (Name = "The B", Type = "Some type", Further_Info = "More Info");
IList<More_B> B_List=new List<More_B>();
var searchObj = B_List.Where(x => x.Name=="The B").FirstOrDefault();
我认为RowFilter中存在错误。由于Line不是列,因此不应在Filter中使用它。也许你正在寻找这样的东西:
view2.RowFilter = "Name like '%" + textBox2.Text + "%' OR Type like '%" + textBox2.Text + "%' OR Further_Info like '%" + textBox2.Text + "%' "