0

For example Listview consists of 5 items.In that 0, 3 items are repeated and 1,4 are also repeated.Now I have to get the distinct items in a textbox from the list of items.These items are static in listview

0-------------A

1-------------B

2-------------C

3-------------A

4-------------B

        List<string[]> list;
        list = new List<string[]>();

          foreach (ListViewItem lvi in listView1.Items)
        {
            string[] values = new string[] { lvi.Text };
            list.Add(values);
        }

From tha above code I filled the list from that list distinct items should display in textbox control

A,B items should be displayed in textbox.

user2897388
  • 85
  • 4
  • 16

2 Answers2

1

Here you go. I assume that each line in your Listview is in the format you provided - number, many dashes and letter at the end. LINQ is quite neat solution for this problem.

// 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 = ...
Ondrej Janacek
  • 12,486
  • 14
  • 59
  • 93
0

Based on your comment in the previous answer (which has been deleted) and based on your statement "A,B items should be displayed in textbox.", i guess what you want is displaying duplicate items in textbox instead of distinct items.

If that is the case, then you can try this (plus you should edit the question):

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;
har07
  • 88,338
  • 12
  • 84
  • 137
  • ..and possible duplicate [here](http://stackoverflow.com/questions/18547354/c-sharp-linq-find-duplicates-in-list) – har07 Dec 02 '13 at 11:00