我在List里面存了很多值,List是按下面的结构放入的
id name sex ,里面存放的情况如下1 222 1
2 333 1
3 555 2
4 888 1
5 333 2
现在我需要吧List里面name为222的取出来 也就是吧“1 222 1”者几个值都取出来怎么做啊,现在急求~

解决方案 »

  1.   

    你这就相当于取数据库配置表的某条数据出来,第一你要有条件去筛选出来,第二你要知道关键之(key)筛选出来
      

  2.   


    List<string> list = new List<string>();
    list.Add("1 222 1");
    ...
    var d = list.Find(p => p.Split(' ')[1] == "222");
    Response.Write(d.ToString());
      

  3.   

    如果是3.5的话可以用Linq
    List<Temp> sss = new List<Temp>();
                sss.Find(x => x.Name = "222");
      

  4.   

    如果list存的对象
    var d = list.Find(p => p.name == "222");
      

  5.   

    你可以用字典或者结构体,这里我以结构体为例写一段代码using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;namespace Test
    {
        public struct Student
        {
            public string Id;
            public string Name;
            public string Sex;
        }    class Program
        {        static void Main(string[] args)
            {
                List<Student> lstStudent = new List<Student>();
                //添加数据
                lstStudent.Add(new Student
                {
                    Id = "1",
                    Name = "1111",
                    Sex = "1"
                });
                lstStudent.Add(new Student
                {
                    Id = "2",
                    Name = "2222",
                    Sex = "2"
                });
                //得到数据方法1:Linq
                var student = lstStudent.AsEnumerable().Where(x => x.Name == "2222").SingleOrDefault();
                Console.WriteLine("Id:" + student.Id);
                Console.WriteLine("Name:" + student.Name);
                Console.WriteLine("Sex:" + student.Sex);
                //得到数据方法2:循环遍历
                foreach (var s in lstStudent)
                {
                    if (s.Name == "2222")
                    {
                        Console.WriteLine("Id:" + s.Id);
                        Console.WriteLine("Name:" + s.Name);
                        Console.WriteLine("Sex:" + s.Sex);
                    }
                }            Console.ReadLine();
            }
        }
    }输出结果为:
    Id:2
    Name:2222
    Sex:2
    Id:2
    Name:2222
    Sex:2
      

  6.   


    楼主说的是List,不是Dictionary<key,value>,从哪来的关键字。
    应该用索引。
      

  7.   

    如果楼主是按照从上到下的顺序插入List 的话,你完全可以使用索引进行读取。索引为0
    list[0]