日期:2014-05-16  浏览次数:20854 次

List<>读取问题
怎么样在一个集合list<>里面,如何最快的查找一条或多条数据
求交流
------解决方案--------------------
List是保存在系统内存里,查询理论上不会慢到哪去
可以用LINQ2Object

var q=list.Where(x=>x.Id==1);
------解决方案--------------------
试试用linq 来查询数据
------解决方案--------------------
4.0及以上可以用Where,Find,或者linq来查询访问
这里贴点简单示例,没遵循语言规范的命名规则,请勿喷……

 class Program
    {
        static void Main(string[] args)
        {
            List<Student> students = new List<Student>();
            students.Add(new Student() {Name="A",Sex="Man",Age="12" });
            students.Add(new Student() { Name = "B", Sex = "Man", Age = "13" });
            students.Add(new Student() { Name = "C", Sex = "Man", Age = "14" });
            students.Add(new Student() { Name = "D", Sex = "Man", Age = "15" });
            //一种访问方法,迭代访问
            foreach (Student student in students)
                Console.WriteLine("Name:"+student.Name+"Sex:"+student.Sex+"Age:"+student.Age+"\n\t");
            //循环输出A B C D的属性
            Console.ReadLine();
            //一种是按所在List数组的索引序号访问
            Console.WriteLine(students[2].Name);
            //输出C
            Console.ReadLine();
            //一种是用linq查询访问
            var s = from student in students
                    where student.Name == "B"
                    select student;
            foreach(Student B in s)
            Console.WriteLine("Name:"+B.Name+"Sex:"+B.Sex+"Age:"+B.Age);
            //输出B ,Man,13
            Console.ReadLine();


            //一种是用Find函数
            Student C = students.Find(x => x.Name == "C");