一,list单个元素比较
List<int> c = new List<int>(); List<int> d = new List<int>(); c.Add(1); c.Add(2); d.Add(1); d.Add(2); d.Add(3);
判断d中是否包含c:
if (c.All(t => d.Any(b => b==t))) { //包含 }else { //不包含 }
二,list 存的是对象
public class Model { public int id { get; set; } public int age { get; set; } public Model(int id,int age) { this.id = id; this.age = age; } }
List<Model> a = new List<Model>(); a.Add(new Model(1,2)); a.Add(new Model(2, 3)); a.Add(new Model(3, 4)); List<Model> b1 = new List<Model>(); b1.Add(new Model(1, 2)); b1.Add(new Model(2, 3)); b1.Add(new Model(3, 4)); b1.Add(new Model(1, 2));
if (a.All(t => b1.Any(b => b.age == t.age && b.id == t.id))) { //包含 }else { //不包含 }