朋友,在我对文件中的所有数据进行排序之后,我有了这种排序方式: e.x 克里斯蒂亚诺·罗纳尔多 克里斯蒂亚诺·罗纳尔多 克里斯蒂亚诺·罗纳尔多 范佩西 卡里姆·本泽马 莱昂内尔·梅西 莱昂内尔·梅西 ... ... 现在,我需要像这样的代码: 克里斯蒂亚诺·罗纳尔多[3] 范佩西[1] 卡里姆·本泽马[1] 利昂·梅西[2] 因此,用相同的名称和Surnme重复多少次才能在方括号中告诉[] ...
Hi friends, After I sort all the data from a file, I have this kind of sort : e.x Cristiano Ronaldo Cristiano Ronaldo Cristiano Ronaldo Van Persi Karim Benzema Lionel Messi Lionel Messi ... ... Now I need the code that this sort make like that : Cristiano Ronaldo [3] Van Persi [1] Karim Benzema [1] Lione Messi [2] So, how many time is repeat the same name and surnme to tell in brackets []...
推荐答案Hello 例如,您有一个名称的数据集.假设它是一个列表(简单): Hello For example you have a DataSet of names. Let''s suppose it''s a List (Simple): List<string> nameList = new List<string>(); nameList.Add("Cristiano Ronaldo"); nameList.Add("Cristiano Ronaldo"); nameList.Add("Cristiano Ronaldo"); nameList.Add("Van Persi"); nameList.Add("Karim Benzema"); nameList.Add("Lione Messi"); nameList.Add("Lione Messi"); nameList.Add("Ali Karimi"); nameList.Add("Ali Karimi"); nameList.Add("Ali Karimi"); nameList = nameList.OrderBy(n => n).ToList();
所以:
So:
Dictionary<string,> nameDictionary = new Dictionary<string,>(); foreach (string s in nameList) { if (nameDictionary.Keys.Contains(s)) { nameDictionary[s]++; continue; } nameDictionary.Add(s, 1); } MyListBox.DataSource = new BindingSource(nameDictionary, null);并且在表单的构造器中(例如MyForm(){...}):
And In the Consructor of Form (For example MyForm(){...}):
MyListBox.Format += new ListControlConvertEventHandler((object sender, ListControlConvertEventArgs e) => { KeyValuePair<string, int> item = (KeyValuePair<string, int>)e.ListItem; e.Value = string.Format("{0} [{1}]", item.Key, item.Value); });
以下代码可用于创建名称的唯一列表以及初始列表中出现的次数. The following code can be used to make distinct list of names along with number of occurances in the initial list. void Main() { Form form1 = new Form(); ListBox listBox1 = new ListBox(); form1.Controls.Add(listBox1); List<string> names = new List<string>(){ "Cristiano Ronaldo", "Lione Messi", "Van Persi", "Karim Benzema", "Lione Messi", "Cristiano Ronaldo", "Cristiano Ronaldo"}; //Make a list of sorted Distinct Names var distinctNames = names.Distinct().OrderBy (n => n); //Make list of distinctNames with count List<string> distinctNamesWithCount = (from name in distinctNames select string.Format("{0} [{1}]",name,names.Count (n => n==name ))).ToList(); listBox1.DataSource=distinctNamesWithCount; form1.ShowDialog(); } //distinctNamesWithCount contents will be //Cristiano Ronaldo [3] //Karim Benzema [1] //Lione Messi [2] //Van Persi [1]
上面的代码可以使用LINQPad进行快速测试,可以从此处下载 www.linqpad/ [ ^ ]
The above code can be tested quickly using LINQPad which can be downloaded from here www.linqpad/[^]
解决方案3不错:) 您也可以尝试这个, Solution 3 is good :) You might try this one as well, using System; using System.Collections.Generic; using System.Linq; namespace Chapter_5 { class Program { static void Main(string[] args) { List<string> names = new List<string>() { "Cristiano Ronaldo", "Lione Messi", "Van Persi", "Karim Benzema", "Lione Messi", "Cristiano Ronaldo", "Cristiano Ronaldo", }; names.GroupBy(name => name).Select(group => string.Format("{0} [{1}]", group.Key, group.Count())) .ToList().ForEach(item => Console.WriteLine(item)); } } }</string></string>
希望对您有所帮助:)
Hope it helps :)
更多推荐
c#再次出现问题!
发布评论