Suppose we have a string of words and we need to figure out how many of them have been repeated.The below code will help to do so
using System;
using System.Collections.Generic;
using System.Linq;
namespace ConsoleApplication
{
class Program
{
static void Main(string[] args)
{
List<string> lstWords = new List<string>() { "hello","this","is","dotnet","dotnet","funda","is","is"};
lstWords .GroupBy(x => x) .Select(g => new { Words = g.Key, WordsRepeated = (g.Count() - 1) == 0
? String.Concat("Word ", g.Key, " has never repeated")
: String.Concat("Word ", g.Key, " repeated ", g.Count(), " times")
}).ToList().ForEach(i => Console.WriteLine("{0} : {1}", i.Words, i.WordsRepeated));
Console.ReadKey(true);
}
}
}