Search code examples
c#winformslinqlistviewexcept

.Except into a listview


I have two lists.

public IList<string> List1 { get; set; }
public IList<string> List2 { get; set; }

I am trying to compare two lists using Except and display the items in a ListView

I have this code to display in the ListView

var list = List1.Except(List2).ToString();
ListViewItem listView = new ListViewItem(list);
this.testListView.Items.Add(listView);

The output I get is "System.Linq.Enumerable+<ExceptIterator>d__72'1[System.String]"

I know I am doing something wrong but cannot figure out what is it. Can anyone point me in the right direction?

Thanks in advance!


Solution

  • Can you try this(assume you want to show one string in one row, though it's strange not use ListBox):

        private void button1_Click(object sender, EventArgs e)
        {
            //add Columns, which you might have done in designer
            //this.testListView.Columns.Add...
            this.testListView.View = View.Details;
            var list = List1.Except(List2).ToList();
            for (int i = 0; i < list.Count; i++)
            {
                ListViewItem item = new ListViewItem(list[i]);
                this.testListView.Items.Add(item);
            }
        }