本文共 1016 字,大约阅读时间需要 3 分钟。
使用Loopup集合类管理相同键值的对象集
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Lookup { class Program { static void Main( string [] args) { Person graham = new Person( " Graham " , " Hill " , " USA " ); Person emerson = new Person( " Emerson " , " Fittipaldi " , " USA " ); Person hc = new Person( " Huang " , " Cong " , " CA " ); List < Person > listPerson = new List < Person > () { graham, emerson, hc }; Lookup < string , Person > Persons = (Lookup < string , Person > )listPerson.ToLookup(r => r.Counary); foreach (Person obj in Persons[ " USA " ]) { Console.WriteLine(obj.FirstName + " " + obj.LastName); } } } public class Person { public string FirstName { get ; set ; } public string LastName { get ; set ; } public string Counary { get ; set ; } public Person( string first, string last, string counary) { FirstName = first; LastName = last; Counary = counary; } } } 输出结果:
本文转自黄聪博客园博客,原文链接:http://www.cnblogs.com/huangcong/archive/2010/05/13/1734605.html,如需转载请自行联系原作者