none
关于IList集合Remove的问题 RRS feed

  • 问题

  • private static IList<Employee> employees = new List<Employee>
    {
               new Employee{Id = "001" , Name = "张三" , Department = "开发部" , Grade = "G7"},
               new Employee{Id = "002" , Name = "李四" , Department = "人事部" , Grade = "G8"},
    };
    public Employee Get(string id)
    {
              Employee employee = employees.FirstOrDefault<Employee>(p => p.Id.Equals(id));
              return employee;
    }
    public void Delete(string id)
     {
                Employee employee = this.Get(id);
                if (null != employee)
                {
                   if(!employees.Remove(employee))
                   {
                       Console.WriteLine("删除失败 :{0}" , employees.Count);
                   }
                   else
                   {
                       Console.WriteLine("删除成功 :{0}" , employees.Count);
                   }
                }
    }


    这样的问题,我感觉好像出现在linq提供的FirstOrDefault 。 请问 , 这是什么原因呢 ? 我今后又该如何有效避免这样的错误呢?
    2014年8月30日 5:56

答案

  • 应该是对的,不知道你之前做了何种操作:

    namespace ConsoleApplication1
    {
        class Employee
        {
            public string Id { get; set; }
            public string Name { get; set; }
            public string Department { get; set; }
        }
    
        class Program
        {
            private static IList<Employee> employees = new List<Employee>
    {
               new Employee{Id = "001" , Name = "张三" , Department = "开发部"},
               new Employee{Id = "002" , Name = "李四" , Department = "人事部"},
    };
    
            public Employee Get(string id)
            {
                Employee employee = employees.FirstOrDefault<Employee>(p => p.Id.Equals(id));
                return employee;
            }
            public void Delete(string id)
            {
                Employee employee = this.Get(id);
                if (null != employee)
                {
                    if (!employees.Remove(employee))
                    {
                        Console.WriteLine("删除失败 :{0}", employees.Count);
                    }
                    else
                    {
                        Console.WriteLine("删除成功 :{0}", employees.Count);
                    }
                }
            }
            static void Main(string[] args)
            {
                Program p = new Program();
                p.Delete("001");
                Console.WriteLine("成功删除:"+employees.Count);
            }
        }
    }

    ASP.NET Forum
    Other Discussion Forums
    FreeRice Donate
    Issues to report
    Free Tech Books Search and Download

    2014年8月30日 9:55
    版主
  • 好奇,你能否在Get中输出取到Employee.查看是否存在重复?
    2014年9月19日 7:01
  • 具体问题找了,是个人失误照成的。
    2014年9月24日 2:04

全部回复

  • 应该是对的,不知道你之前做了何种操作:

    namespace ConsoleApplication1
    {
        class Employee
        {
            public string Id { get; set; }
            public string Name { get; set; }
            public string Department { get; set; }
        }
    
        class Program
        {
            private static IList<Employee> employees = new List<Employee>
    {
               new Employee{Id = "001" , Name = "张三" , Department = "开发部"},
               new Employee{Id = "002" , Name = "李四" , Department = "人事部"},
    };
    
            public Employee Get(string id)
            {
                Employee employee = employees.FirstOrDefault<Employee>(p => p.Id.Equals(id));
                return employee;
            }
            public void Delete(string id)
            {
                Employee employee = this.Get(id);
                if (null != employee)
                {
                    if (!employees.Remove(employee))
                    {
                        Console.WriteLine("删除失败 :{0}", employees.Count);
                    }
                    else
                    {
                        Console.WriteLine("删除成功 :{0}", employees.Count);
                    }
                }
            }
            static void Main(string[] args)
            {
                Program p = new Program();
                p.Delete("001");
                Console.WriteLine("成功删除:"+employees.Count);
            }
        }
    }

    ASP.NET Forum
    Other Discussion Forums
    FreeRice Donate
    Issues to report
    Free Tech Books Search and Download

    2014年8月30日 9:55
    版主
  • 好奇,你能否在Get中输出取到Employee.查看是否存在重复?
    2014年9月19日 7:01
  • 具体问题找了,是个人失误照成的。
    2014年9月24日 2:04