Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace ConsoleApplication17
- {
- class Program
- {
- public static IEnumerable<IEnumerable<T>> GroupBy<T, F>(IEnumerable<T> collection, Func<T, F> rule)
- {
- var dicResult = new Dictionary<F, List<T>>();
- foreach(var e in collection)
- {
- var functionValue = rule(e);
- if (!dicResult.ContainsKey(functionValue))
- {
- dicResult.Add(functionValue, new List<T> { e});
- }
- else
- {
- dicResult[functionValue].Add(e);
- }
- }
- return dicResult.Values ;
- }
- static void Main(string[] args)
- {
- var list = new List<string>()
- {
- "abc","bcd","d","f","gfd","2","fsdfsdfsdf","b", "gaa", "22"
- };
- Func<string, int> f = line => line.Length;
- Func<string, char> g = line => line[0];
- var result = GroupBy(list, f);
- var result2 = GroupBy(list, g);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement