C# 如何在windows应用程序中从listview中获取不同的项

C# 如何在windows应用程序中从listview中获取不同的项,c#,C#,例如,Listview由5个项目组成。其中0个项目重复,3个项目重复,1,4个项目也重复。现在,我必须从项目列表中获取文本框中的不同项目。这些项目在Listview中是静态的 0--------A 1-------B 2-------C 3---------A 4-------B 列表; 列表=新列表(); foreach(listView1.Items中的ListViewItem lvi) { 字符串[]值=新字符串[]{lvi.Text}; 列表。添加(值); } 根据上面的代码,我填写了

例如,Listview由5个项目组成。其中0个项目重复,3个项目重复,1,4个项目也重复。现在,我必须从项目列表中获取文本框中的不同项目。这些项目在Listview中是静态的

0--------A

1-------B

2-------C

3---------A

4-------B

列表;
列表=新列表();
foreach(listView1.Items中的ListViewItem lvi)
{
字符串[]值=新字符串[]{lvi.Text};
列表。添加(值);
}
根据上面的代码,我填写了列表,不同的项目应该显示在文本框控件中


A、 B项应显示在文本框中。

根据您在上一个答案(已删除)中的评论以及您的陈述“A,B项应显示在文本框中”。我猜您想要的是在文本框中显示重复项,而不是不同项

如果是这种情况,那么您可以尝试以下方法(另外,您应该编辑问题):

List List=(从listView1.Items中的ListViewItem lvi选择lvi.Text);
var duplicateQuery=list.GroupBy(text=>text)
.Where(group=>group.Count()>1)
.Select(group=>group.Key);
字符串duplicateList=“”;
bool start=true;
foreach(在a中重复变量)
{
如果(启动)
{
开始=错误;
重复列表+=重复;
}
else duplicateList+=“,”+重复;
}
TextBox1.Text=重复列表;

给你。我假设Listview中的每一行都是您提供的格式—数字、许多破折号和结尾的字母。对于这个问题,LINQ是一个非常简洁的解决方案

// take only the last character which is a letter (A, B, ...)
var letterList = list.Select(i => i.Last());
// group the letters and select those which are present more than once
var dups = letterList.GroupBy(i => i)
                     .Where(i => i.Count() > 1)
                     .Select(i => i.Key);
// take the resulting letter and join them with the ',' character
var result = string.Join(",", dups); // or textBox.Text = ...
…和可能的重复
List<string> list = (from ListViewItem lvi in listView1.Items select lvi.Text).ToList();

var duplicateQuery = list.GroupBy(text => text)
    .Where(group => group.Count() > 1)
            .Select(group => group.Key);
string duplicateList = "";
bool start = true;
foreach (var duplicate in a)
{
    if (start)
    {
        start = false;
        duplicateList += duplicate;
    }
    else duplicateList += ", " + duplicate;
}
TextBox1.Text = duplicateList;
// take only the last character which is a letter (A, B, ...)
var letterList = list.Select(i => i.Last());
// group the letters and select those which are present more than once
var dups = letterList.GroupBy(i => i)
                     .Where(i => i.Count() > 1)
                     .Select(i => i.Key);
// take the resulting letter and join them with the ',' character
var result = string.Join(",", dups); // or textBox.Text = ...