本文介绍了如何编辑集合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的朋友们,

我有一些问题,

现在我有一个字符串集合但该集合有多个相同的项目,我的愿望,找到相同的项目并添加删除其中一个后的计数器。



Hi dear friends,
I have some question,
Now I have a string collection but that collection has multiple same item, my wish , find same item and add counter after remove one of the.

MatchCollection matches = Regex.Matches(text.ToLower(), pattern);
                    for (int i = 0; i < matches.Count; i++)
                    {
                        string s = "<li>" + matches[i] + "</li>";
                        string[] stringArray = s.Split(' ');
                        if (stringArray.Length>1)
                        {
                            for (int j = 0; j < matches.Count; j++)
                            {
                                if (matches[i].ToString() == matches[j].ToString() && i == j)
                                {
                                    counter++;
                                }
                            }
                            var newWord=new Vocabulary {Word = matches[i].ToString(),Frequency = counter};
                            counter = 0;
                            listSentences.Add(newWord);
                        }
                    }





这是我的代码,但根据我没有工作。

我的错误在哪里?



谢谢你



This is my Code but not working according to me.
Where is the my mistake?

Thank you

推荐答案


class Program
   {
       static void Main(string[] args)
       {
           var s1 = new List<string> { "Item1", "Item2", "Item1", "Item2" };
           var finalResult = new List<FinalResult>();
           foreach (var item in s1.Distinct())
           {
               string item1 = item;
               var data = s1.Where(x => x == item1);
               if(data.Count()>1)
               {
                   finalResult.Add(new FinalResult{ItemName = item1,Count = data.Count()});
               }
           }

           Console.ReadLine();
       }
       public class FinalResult
       {
           public string ItemName { get; set; }
           public int Count { get; set; }
       }
   }



希望这有助于


Hope this helps


这篇关于如何编辑集合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-02 20:43