Two C # List <TModel> acquired in different objects according to the specified conditions

Two C # List <TModel> acquired in different objects according to the specified conditions

 public class Test
    {
        public int age { get; set; }
        public string name { get; set; }
        public int score { get; set; }
    }
            List<Test> list1 = new List<Test>();
            list1.Add(new Test { score = 10, name = "001" });
            list1.Add(new Test { score = 20, name = "002" });
            list1.Add(new Test { score = 30, name = "003" });
            list1.Add(new Test { score = 40, name = "004" });
            list1.Add(new Test { score = 50, name = "005" });
            list1.Add(new Test { score = 60, name = "005" });

            List<Test> list2 = new List<Test>();
            list2.Add(new Test { score = 10, name = "001" });
            list2.Add(new Test { score = 20, name = "002" });
            list2.Add(new Test { score = 30, name = "003" });
            list2.Add(new Test { score = 40, name = "004" });

            //list3 return 2
            List<Test> list3 = list1.Where(x => !list2.Any(x2 => x.score == x2.score)).ToList();
            //list4 return 2
            List<Test> list4 = list1.Where(x => list2.All(x2 => x.score != x2.score)).ToList();

            MessageBox.Show("list3==" + list3.Count+"\r\nlist4==" + list4.Count);     

 

Guess you like

Origin www.cnblogs.com/YYkun/p/11639802.html