1.创建一个学生类Student,有如下属性:StuID,Name,Sex,Age
2.创建四个学生对象,张三,李四,王五,赵六
3.创建泛型集合Dictionary<Int,Student>对象,把StuID作为键,学生对象作为值加入Dictionary<Int,Student>对象里。
4.创建一个SelStudent.aspx网页,拖一个文本框和一个查询按纽到页面上,再拖一个ListBox控件,在文本框中输入学生编号,点击查询,在泛型集合中查询详细信息, ListBox中显示该学生的详细信息。体会两种查询的区别。
你这好像是作业吧!
最后说“两种查询的区别”是哪两种啊,按你描述的只有按学生编号从Dictionary中查询学生信息,还有一种是什么啊?
写了个控制台程序:
public class BWTest
{
Dictionary<int, Student> stuDictionary = new Dictionary<int, Student>()
{
{1,new Student{StuID=1,Name="张三",Sex=true,Age=23}},
{2,new Student{StuID=2,Name="李四",Sex=true,Age=24}},
{3,new Student{StuID=3,Name="王五",Sex=true,Age=25}},
{4,new Student{StuID=4,Name="赵六",Sex=false,Age=26}}
};
List<Student> stuList = new List<Student>()
{
new Student{StuID=1,Name="张三",Sex=true,Age=23},
new Student{StuID=2,Name="李四",Sex=true,Age=24},
new Student{StuID=3,Name="王五",Sex=true,Age=25},
new Student{StuID=4,Name="赵六",Sex=false,Age=26}
};
public void DicFind(int SutID)
{
Console.WriteLine("Dictionary方式查找:");
var o = stuDictionary.Where(p =>p.Key==SutID).Select(p=>p.Value);
OutPut(o);
}
public void ListFind(int StuID)
{
Console.WriteLine("List方式查找:");
var o = stuList.Where(p=>p.StuID==StuID);
OutPut(o);
}
private void OutPut(IEnumerable<Student> o)
{
if (o.Count() > 0)
{
var temp = o.ElementAt(0);
Console.WriteLine("StuID: " + temp.StuID);
Console.WriteLine("Name: " + temp.Name);
Console.WriteLine("Sex: " + (temp.Sex ? "男" : "女"));
Console.WriteLine("Age: " + temp.Age);
}
else
{
Console.WriteLine("no result");
}
Console.WriteLine();
}
}
public class Student
{
public int StuID { get; set; }
public string Name { get; set; }
public bool Sex { get; set; }
public int Age { get; set; }
}
static void Main(string[] args)
{
BWTest bwt = new BWTest();
Console.Write("Please input student StuID(input -1 exit):");
int StuID =Int32.Parse(Console.ReadLine());
while (StuID != -1)
{
bwt.DicFind(StuID);
bwt.ListFind(StuID);
Console.Write("Please input student StuID(input -1 exit):");
StuID = Int32.Parse(Console.ReadLine());
}
Console.Write("Press any key close");
Console.Read();
}