using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace mapReduce { public static class helper { public static Dictionary<TKey, TResult> MapReduce<TInput, TKey, TValue, TResult>( this IEnumerable<TInput> list, //Func<TInput, IEnumerable<KeyValuePair<TKey, TValue>>> map, Func<TInput, KeyValuePair<TKey, TValue>> map, Func<TKey, IEnumerable<TValue>, TResult> reduce) { Dictionary<TKey, List<TValue>> mapResult = new Dictionary<TKey, List<TValue>>(); foreach (var item in list) { //foreach (var one in map(item)) //{ var one = map(item); if (Convert.ToInt32( one.Key) == 0) { continue; } List<TValue> mapValues; if (!mapResult.TryGetValue(one.Key, out mapValues)) { mapValues = new List<TValue>(); mapResult.Add(one.Key, mapValues); } mapValues.Add(one.Value); //} } var result = new Dictionary<TKey, TResult>(); foreach (var m in mapResult) { result.Add(m.Key, reduce(m.Key, m.Value)); } return result; } } }
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace mapReduce { class Program { static void Main(string[] args) { List<Person> list = new List<Person>(); list.Add(new Person { ID = 1, Name = "肖银龙", Age = 23 }); list.Add(new Person { ID = 2, Name = "郭彦磊", Age = 24 }); list.Add(new Person { ID = 3, Name = "王老师", Age = 23 }); list.Add(new Person { ID = 4, Name = "路人甲", Age = 25 }); list.Add(new Person { ID = 5, Name = "路人乙", Age = 20 }); var result = list.MapReduce<Person, int, string, string>(Map, (key, values) => string.Join(",", values)); foreach (var d in result) { Console.WriteLine(d.Key + ":" + d.Value); } Console.Read(); } //public static IEnumerable<KeyValuePair<int, string>> Map(Person p) //{ // if (p.Age > 22) // yield return new KeyValuePair<int, string>(p.Age, p.Name); //} public static KeyValuePair<int, string> Map(Person p) { if (p.Age > 22) return new KeyValuePair<int, string>(p.Age, p.Name); else return new KeyValuePair<int, string>(0,null); } } public class Person { public int ID { get; set; } public string Name { get; set; } public int Age { get; set; } } }
原文:http://blog.csdn.net/xyl295528322/article/details/28886093