C# 如何查询具有相同值的类 请考虑我的英语不好。
类“Student”的列表绑定到ListBox,Displaymemeber是“group”字段 我的目标是不重叠地向团队展示。(仅A和B) 但这里我的代码显示如下 如何仅显示A和BC# 如何查询具有相同值的类 请考虑我的英语不好。,c#,class,data-binding,listbox,C#,Class,Data Binding,Listbox,类“Student”的列表绑定到ListBox,Displaymemeber是“group”字段 我的目标是不重叠地向团队展示。(仅A和B) 但这里我的代码显示如下 如何仅显示A和B public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e)
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
Student st1 = new Student("Kim", 15, Group.A);
Student st2 = new Student("Lee", 15, Group.A);
Student st3 = new Student("Park", 15, Group.B);
Student.Add(st1);
Student.Add(st2);
Student.Add(st3);
listBox1.DataSource = Student.LstStudent;
listBox1.DisplayMember = "group";
}
}
public enum Group
{
None,
A,
B,
C
}
public class Student
{
private static List<Student> _LstStudent = new List<Student>();
public static List<Student> LstStudent
{
get
{
return _LstStudent;
}
}
public Group group { get; set; }
public string name { get; set; }
public int age { get; set; }
public Student(string name, int age, Group group)
{
this.name = name;
this.age = age;
this.group = group;
}
public static void Add(Student student)
{
LstStudent.Add(student);
}
}
公共部分类表单1:表单
{
公共表格1()
{
初始化组件();
}
私有void Form1\u加载(对象发送方、事件参数e)
{
学生st1=新生(“Kim”,15岁,A组);
学生st2=新生(“Lee”,15岁,A组);
学生st3=新生(“公园”,15,B组);
学生。添加(st1);
学生。添加(st2);
学生。添加(st3);
listBox1.DataSource=Student.LstStudent;
listBox1.DisplayMember=“组”;
}
}
公共枚举组
{
没有一个
A.
B
C
}
公立班学生
{
私有静态列表lstustudent=new List();
学生的公共静态列表
{
得到
{
返回学生;
}
}
公共组组{get;set;}
公共字符串名称{get;set;}
公共整数{get;set;}
公立学生(字符串名称、整数年龄、组)
{
this.name=名称;
这个。年龄=年龄;
this.group=组;
}
公共静态无效添加(学生)
{
lststuden.Add(学生);
}
}
如果您只想显示组,则可以使用LINQ筛选列表
试着改变
listBox1.DataSource = Student.LstStudent;
listBox1.DisplayMember = "group";
到
如果只显示组,则可以使用LINQ筛选列表
试着改变
listBox1.DataSource = Student.LstStudent;
listBox1.DisplayMember = "group";
到
@YeongSeokOh我已经更新了我的答案,包括.ToArray()
方法链。我不确定DataSource
属性是需要数组还是仅仅需要IEnumerable
。如果现在回答了您的问题,请接受答案,谢谢!我能再问一个吗?如何才能得到该组的所有学生?@YeongSeokOh我已经更新了我的答案,包括.ToArray()
方法链。我不确定DataSource
属性是需要数组还是仅仅需要IEnumerable
。如果现在回答了您的问题,请接受答案,谢谢!我能再问一个吗?怎样才能得到那个小组的所有学生呢?