c#再次出现问题!

编程入门 行业动态 更新时间:2024-10-10 13:13:37
本文介绍了c#再次出现问题!的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

朋友,在我对文件中的所有数据进行排序之后,我有了这种排序方式: 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#再次出现问题!

本文发布于:2023-10-27 23:49:29,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1534887.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!