我有班级学生:
public class Student
{
public string Name { get; set; }
public string Age { get; set; }
public Student()
{
}
public List<Student> getData()
{
List<Student> st = new List<Student>
{
new Student{Name="Pham Nguyen",Age = "22"},
new Student{Name="Phi Diep",Age = "22"},
new Student{Name="Khang Tran",Age = "28"},
new Student{Name="Trong Khoa",Age = "28"},
new Student{Name="Quan Huy",Age = "28"},
new Student{Name="Huy Chau",Age = "28"},
new Student{Name="Hien Nguyen",Age = "28"},
new Student{Name="Minh Sang",Age = "28"},
};
return st;
}
}
如何在此课程中学习数据? (我的意思是 - 例子:我想拿Name =“Minh Sang”,年龄=“28”来表示。)
抱歉这个问题。但我不知道在哪里找到它。感谢所有
答案 0 :(得分:2)
您可以使用linq:
Student st = new Student();
var getStudent = from a in st.getData()
where a.Age == "28" & a.Name == "Minh Sang"
select a;
MessageBox.Show(getStudent.First().Age);
MessageBox.Show(getStudent.First().Name);
答案 1 :(得分:0)
查看List.Find方法:
http://msdn.microsoft.com/en-us/library/x0b5b5bc.aspx
接下来尝试实施一种新方法:
public Student GetStudent(string name, int age)
答案 2 :(得分:0)
调用getData()获取学生列表。
使用foreach循环遍历列表。
在循环内,打印出学生的姓名和年龄。
答案 3 :(得分:0)
也许您正在寻找DebuggerDisplay属性以在调试器中显示它?
[DebuggerDisplay("Name = {name}, Age={age}")]
public class Student {....}
因此,当您将鼠标悬停在Student类型的项目上时,它将以您想要的方式显示...
答案 4 :(得分:0)
编辑1: 将这些方法添加到您的班级:
public Student getStudent(int age, string name)
{
return this.getData().Find(s => Convert.ToInt32(s.Age) == age && s.Name.Equals(name));
}
public Student getByIndex(int index)
{
Student s = null;
// maxIndex will be: 7
// your array goes from 0 to 7
int maxIndex = this.getData().Count() - 1;
// If your index does not exceed the elements of the array:
if (index <= maxIndex)
s = this.getData()[index];
return s;
}
int
或>
进行评估,我会将年龄转换为<
。编辑2: 然后调用这样的方法:
Student st = new Student();
// s1 and s2 will return null if no result found.
Student s1 = st.getStudent(28, "Minh Sang");
Student s2 = st.getByIndex(7);
if (s1 != null)
Console.WriteLine(s1.Age);
Console.WriteLine(s1.Name);
if (s2 != null)
Console.WriteLine(s2.Age);
Console.WriteLine(s2.Name);