linq - Count string appears in list and copy along with number of time it appear into strongly type list- c# -


i have list of string containing different words , of words repeating. want copy string list , save type list string , no of times appears in list. need use linq.

model class

 public class wordcount {     public wordcount()     { }      public int id { get; set; }      public string word { get; set; }      public int counter { get; set; } } 

my current list

 private list<string> _wordcontent = new list<string>(); 

new type list ?????????

 public void countwordinwebsitecontent()     {          var counts = _wordcontent                     .groupby(w => w)                     .select(g => new { word = g.key, count = g.count() })                     .tolist();           list<wordcount> mywordlist = new list<wordcount>();            var = "d";     } 

you can create objects of wordcount while projecting grouped result:

here using lambda expression syntax:

.select((g,index) => new wordcount                          {                             word = g.key,                            counter = g.count(),                            id =index          }).tolist(); 

using query expression syntax:

int id=1; var counts  = word in _wordcontent               group word word g                select new wordcount                          {                              id= id++,                             word = g.key,                             counter = g.count()                           }; 

Comments

Popular posts from this blog

python - TypeError: start must be a integer -

c# - DevExpress RepositoryItemComboBox BackColor property ignored -

django - Creating multiple model instances in DRF3 -