Using IEnumerable.Except

I got 3 listViews 2 textbox and 2 buttons in WinForm.

Description of the program: the program adds numbers to listview by entering numbers in the textbox and pressing the button button

Purpose: I want to use the IEnumerable.Except method to output only unique numbers in listView3 , for example, in the figure below, unique numbers are 3 and 7 in listView1 and listView2 . ListViewItem lvi = new ListViewItem (textBox1.Text); listView1.Items.Add (LVI);

 ListViewItem lv = new ListViewItem(textBox2.Text); listView2.Items.Add(lv); //im doing somthing wrong here... var nonintersect = listView1.Except(listView2).Union(listView2.Except(listView1)); //populate listview3 with the unique numbers... // foreach (item ) // { // } 

Error message: System.Windows.Forms.ListView 'does not contain a definition for "Except" and does not use the extension method "Except" for accepting the first argument of the type "System.Windows.Forms.ListView" that can be found (you do not have a directive using or assembly reference?)

enter image description here

+4
source share
4 answers

He called "Symmetric Diversity" and it's simple.

 var nonintersect = listView1.Except(listView2).Union(listView2.Except(listView1)); 

Source of origin

+5
source

You cannot do this with Except alone, as it will return {3} or just {7} in your example. However, if you take the given intersection between the two {1, 2, 4}, and then use Except , you can get de-intersection (basically this is what you are looking for):

 IEnumerable<int> allObjects = list1.Concat(list2); IEnumerable<int> intersection = list1.Intersect(list2); IEnumerable<int> deIntersection = allObjects.Except(intersection); 
+1
source

You will need to do something like this:

 IEnumerable<string> common = Enumerable .Intersect( listView1.Items.Cast<ListViewItem>().Select(x => x.Text), listView2.Items.Cast<ListViewItem>().Select(x => x.Text)); IEnumerable<string> all = Enumerable .Union( listView1.Items.Cast<ListViewItem>().Select(x => x.Text), listView2.Items.Cast<ListViewItem>().Select(x => x.Text)); IEnumerable<string> unique = all.Except(common); 
0
source

Except not defined for ListView . This is actually an extension method that is defined in System.Linq.Enumerable . You are probably confused because you can call Except from IList<T> (since IList<T> comes from IEnumerable<T> ), but what you are trying to do will never work, because ListView doesn't inherits from IEnumerable<T> . The exception you get is the correct behavior.

To get the Except behavior for your controls, you will need to manage the database and then bind the resulting collection object to the ListView control.

0
source

All Articles