------------------------------------------------------------------------------Person.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
public class Person
{
public string Country { get; private set; }
public string Name { get; private set; }
public int Age { get; private set; }
public Person(string country, string name, int age)
{
this.Country = country;
this.Name = name;
this.Age = age;
}
}
}------------------------------------------------------------------------------主程序
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
//可以用于实现了IEnumerable<T>的所有类
//一个键关联多个值
List<Person> lp = new List<Person>();
lp.Add(new Person("中国", "张飞", 40));
lp.Add(new Person("中国", "关羽", 43));
lp.Add(new Person("中国", "刘备", 45));
lp.Add(new Person("中国", "诸葛亮", 24));
lp.Add(new Person("美国", "豪威尔", 40));
lp.Add(new Person("美国", "奥巴马", 40));
lp.Add(new Person("朝鲜", "金三胖", 40));
lp.Add(new Person("印度", "印度阿三", 40));
//传入一个Person对象,返回string
var t = lp.ToLookup(r => r.Country);//键为Country(国家)
foreach (Person item in t["中国"])//找到所有为中国的Person对象
{
Console.WriteLine(item.Name);
}
Console.ReadKey();
}
}
}本文出自 “程序猿的家” 博客,请务必保留此出处http://962410314.blog.51cto.com/7563109/1548031
原文:http://962410314.blog.51cto.com/7563109/1548031