首页 新闻 会员 周边

创建泛型集合Dictionary<Int,Student>对象

0
悬赏园豆:10 [已解决问题] 解决于 2011-02-25 23:17

1.创建一个学生类Student,有如下属性:StuID,Name,Sex,Age

2.创建四个学生对象,张三,李四,王五,赵六

3.创建泛型集合Dictionary<Int,Student>对象,把StuID作为键,学生对象作为值加入Dictionary<Int,Student>对象里。

4.创建一个SelStudent.aspx网页,拖一个文本框和一个查询按纽到页面上,再拖一个ListBox控件,在文本框中输入学生编号,点击查询,在泛型集合中查询详细信息, ListBox中显示该学生的详细信息。体会两种查询的区别。

蝸牛漫步的主页 蝸牛漫步 | 初学一级 | 园豆:0
提问于:2011-02-24 14:52
< >
分享
最佳答案
0

你这好像是作业吧!

最后说“两种查询的区别”是哪两种啊,按你描述的只有按学生编号从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();
}

收获园豆:10
artwl | 专家六级 |园豆:16736 | 2011-02-24 15:00
List<>
蝸牛漫步 | 园豆:0 (初学一级) | 2011-02-24 15:11
写了个控制台程序(见上面回答),你可以参考一下,然后改为web形式
artwl | 园豆:16736 (专家六级) | 2011-02-24 16:52
呵呵!我做出来了
蝸牛漫步 | 园豆:0 (初学一级) | 2011-02-24 17:52
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册