Linq 对List的一些操作
代碼:
public class Person{public int ID { get; set; }public string Name { get; set; }public int Age { get; set; } }public class Dog{public int ID { get; set; }public string Name { get; set; }public int PersonID { get; set; }}public class DogClothes{public int ID { get; set; }public string Name { get; set; }public int DogID { get; set; }}public class ActionPerson{ public List<Person> GetPersons(){List<Person> list = new List<Person>();for (var i = 0; i < 10; i++){Person p = new Person {ID=i+1, Name = "hongda" + (i + 1), Age = 26 + i };list.Add(p);}return list;}public List<Dog> GetDogs(){List<Dog> dogs = new List<Dog>();for (var i = 1; i < 15; i++){Dog dog = new Dog { ID = i, Name = "dogs" + i, PersonID = (i %5)+1 };dogs.Add(dog);}return dogs;}public List<DogClothes> GetDogClotheses(){List<DogClothes> DogClothes = new List<DogClothes>();for (var i = 1; i < 13; i++){DogClothes clothes = new DogClothes() { ID = i, Name = "DogClothes" + i, DogID = (i % 4) + 2 };DogClothes.Add(clothes);}return DogClothes;}public void Show<T>(List<T> list){foreach (var l in list){ShowObj(l);}}public void ShowObj<T>(T data){Type t = data.GetType();PropertyInfo[] infos = t.GetProperties();StringBuilder strBuilder = new StringBuilder();foreach (var p in infos){object value = p.GetValue(data, null);string name = p.Name.ToString();strBuilder.Append(name + ":" + value + ",");}Console.WriteLine(strBuilder.ToString());}}?
對(duì)list操作
上面的代碼可以不看,反正就是一些臨時(shí)用的數(shù)據(jù)
ActionPerson ap = new ActionPerson();List<Person> list = ap.GetPersons();ap.Show(list);Console.WriteLine("========================================");List<Dog> dogs = ap.GetDogs();ap.Show(dogs);Console.WriteLine("========================================");上面的是list,下面的是dogs
ap(dogClothes);
?1.取有狗的人
var list2 = from a in list from b in dogs where a.ID == b.PersonID select a;ap.Show(list2.ToList());Console.WriteLine("========================================");ap.Show(list2.Distinct().ToList());Console.WriteLine("========================================");發(fā)現(xiàn),自連接就是左表一條條數(shù)據(jù)與右表一條條數(shù)據(jù)交叉,取得符合條件的(a.ID==b.PersonID),再取得需要的字段(a),不會(huì)自動(dòng)合并。
看這個(gè)就清楚了
var list4 = from a in list from b in dogs where a.ID == b.PersonID select new { a.ID, a.Name, a.Age, DogsID = b.ID, DogsName = b.Name };ap.Show(list4.ToList());這樣上面的就不要區(qū)分(distinct)了,之后再取需要的字段
2.join
var list2 = from a in list join b in dogs on a.ID equals b.PersonID into c select new { a.ID, a.Name, a.Age,Count=c.Count() };ap.Show(list2.ToList());?在上面這種into一個(gè)對(duì)象時(shí),會(huì)發(fā)現(xiàn)b不能夠調(diào)用了,a還是可以使用的,當(dāng)然新形成的對(duì)象c也可以,c有一些特殊的屬性,例如count
var list2 = from a in list join b in dogs on a.ID equals b.PersonID select new { a.ID, a.Name, a.Age, DogsID = b.ID, DogsName = b.Name, b.PersonID }; var list2 = from a in list join b in dogs on a.ID equals b.PersonID select new { a.ID, a.Name, a.Age };ap.Show(list2.ToList());跟自連接一樣
?LINQ TO SQL中的join,如果帶有into,可以提前對(duì)表進(jìn)行過濾條件的操作,而不用等到兩表進(jìn)行迪卡爾積產(chǎn)生虛似表后再進(jìn)行join on的條件過濾。
?關(guān)于left join
var list2 = from a in listjoin b in dogs on a.ID equals b.PersonID into cfrom o in c.DefaultIfEmpty()select new{a.ID,a.Name,a.Age,DogsID =o==null?0: o.ID,DogsName = o == null ? "" : o.Name,PersonID=o==null?0:o.PersonID };?在數(shù)據(jù)庫中就不需要這么復(fù)雜,這里list中要考慮右側(cè)可能為空,
數(shù)據(jù)庫查詢就不需要。
?多查詢
static void Main(string[] args){ActionPerson ap = new ActionPerson();List<Person> list = ap.GetPersons();List<Dog> dogs = ap.GetDogs();List<DogClothes> dogClotheses = ap.GetDogClotheses();var result = from a in listjoin b in dogson a.ID equals b.PersonIDjoin c in dogClotheseson b.ID equals c.DogIDselect new{a.ID ,a.Name ,a.Age,DogID=b.ID ,DogName=b.Name,b.PersonID ,DogClothesID=c.ID,DogClothesName=c.Name ,DogID2=c.DogID };ap.Show(result.ToList ()); Console.ReadLine();}?
var result = from a in listfrom b in dogsfrom c in dogClotheseswhere a.ID == b.PersonID && b.ID == c.DogIDselect new{a.ID,a.Name,a.Age,DogID = b.ID,DogName = b.Name,b.PersonID,DogClothesID = c.ID,DogClothesName = c.Name,DogID2 = c.DogID };結(jié)果上同
http://developer.51cto.com/art/200909/152189.htm
http://www.cnblogs.com/ASPNET2008/archive/2008/12/21/1358152.html
與50位技術(shù)專家面對(duì)面20年技術(shù)見證,附贈(zèng)技術(shù)全景圖總結(jié)
以上是生活随笔為你收集整理的Linq 对List的一些操作的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 先安装证书服务(CA),再安装IIS,导
- 下一篇: 使用gdbserver远程调试