private void foo1() { RootObject testObject = new RootObject() { RootIntElement = 1, RootStringElement = "test", Childs = new List <ChildObject>() }; testObject.Childs.Add(new ChildObject() { ChildIntElement = 11, ChildStringElement = "Child1" }); testObject.Childs.Add(new ChildObject() { ChildIntElement = 12, ChildStringElement = "Child2" }); testObject.Childs.Add(new ChildObject() { ChildIntElement = 13, ChildStringElement = "Child3" }); testObject.Childs.Add(new ChildObject() { ChildIntElement = 14, ChildStringElement = "Child4" }); testObject.Childs.Add(new ChildObject() { ChildIntElement = 15, ChildStringElement = "Child5" }); List <ChildObject> targets = new List <ChildObject>(); targets.Add(new ChildObject() { ChildIntElement = 11, ChildStringElement = "TargetChild1" }); targets.Add(new ChildObject() { ChildIntElement = 11, ChildStringElement = "TargetChild2" }); targets.Add(new ChildObject() { ChildIntElement = 11, ChildStringElement = "TargetChild3" }); targets.Add(new ChildObject() { ChildIntElement = 14, ChildStringElement = "TargetChild4" }); targets.Add(new ChildObject() { ChildIntElement = 16, ChildStringElement = "TargetChild5" }); testObject.Childs.ForEach(l => { l.Childs = targets.Where(p => p.ChildIntElement.ToString() == l.ChildIntElement.ToString()).ToList(); }); var result1 = targets.Find(u => u.ChildIntElement == 10); var result2 = targets.First(u => u.ChildIntElement == 110); //throw exception!! var result3 = targets.FirstOrDefault(u => u.ChildIntElement == 10); var result4 = targets.FindAll(u => u.ChildIntElement == 11); var result5 = targets.Any(u => u.ChildIntElement == 15); int pageindex = 10; int pagesize = 2; var result6 = targets.Skip((pageindex - 1) * pagesize).Take(pagesize).ToList(); var result7 = targets.Select(o => o.ChildStringElement).Distinct() .Select((o, i) => string.Format("{0}. {1}", i + 1, o)).ToList(); ChildObject FirstOrDefaultResult = targets.FirstOrDefault(u => u.ChildIntElement == 24); }
private void foo5() { List <RootObject> RootObjectList = new List <RootObject> { //root1: 对象完整 new RootObject() { RootIntElement = 1, RootStringElement = "root1", Childs = new List <ChildObject> { new ChildObject() { ChildIntElement = 11, ChildStringElement = "Child11" }, new ChildObject() { ChildIntElement = 12, ChildStringElement = "Child12" }, new ChildObject() { ChildIntElement = 13, ChildStringElement = "Child13" }, } }, //root2: 对象完整 new RootObject() { RootIntElement = 2, RootStringElement = "root2", Childs = new List <ChildObject> { new ChildObject() { ChildIntElement = 21, ChildStringElement = "Child21" }, new ChildObject() { ChildIntElement = 22, ChildStringElement = "Child22" }, new ChildObject() { ChildIntElement = 23, ChildStringElement = "Child23" } } }, //root3: 对象内String为null new RootObject() { RootIntElement = 3, RootStringElement = null, Childs = new List <ChildObject>() }, //root4: 对象内ojbect为null new RootObject() { RootIntElement = 4, RootStringElement = "root4", Childs = null }, }; var result1 = RootObjectList.Select(l => l.RootIntElement); var result2 = RootObjectList.Select(l => l.RootStringElement); var result3 = RootObjectList.Where(l => l.RootIntElement == 21); var result4 = RootObjectList.FindAll(l => l.RootIntElement == 21); var result5 = RootObjectList.SelectMany(l => l.Childs ?? new List <ChildObject>()).ToList(); RootObject result6 = RootObjectList.First(); //RootObject result6 = RootObjectList.First(a => a.RootIntElement == 20); 查不到将报错 RootObject result7 = RootObjectList.FirstOrDefault(a => a.RootIntElement == 99); var result8 = RootObjectList.Sum(l => l.RootIntElement); var result9 = RootObjectList.Where(m => m.Childs != null).Sum(l => l.Childs.Sum(m => m.ChildIntElement)); var result10 = RootObjectList.OrderByDescending(o => o.RootIntElement); result10.First().RootStringElement = "test!!!"; List <ChildObject> joinTargets = new List <ChildObject>() { new ChildObject() { ChildIntElement = 11, ChildStringElement = "TargetChild11" }, new ChildObject() { ChildIntElement = 22, ChildStringElement = "TargetChild22" }, new ChildObject() { ChildIntElement = 34, ChildStringElement = "TargetChild34" }, new ChildObject() { ChildIntElement = 36, ChildStringElement = "TargetChild36" } }; RootObjectList.ForEach(l => { if (l.Childs != null) { l.Boys = (from c in joinTargets join u in l.Childs on c.ChildIntElement equals u.ChildIntElement select c).ToList(); l.Boys.ForEach(a => a.ChildStringElement = "test!!!"); } }); }