在C#中使用IEnumerable除外 [英] Using IEnumerable Except in C#
问题描述
我在WinForm中有3个listViews 2文本框和2个按钮。
程序描述:程序通过键入将数字添加到列表视图在文本框中的数字并单击添加按钮
目标:我希望能够使用IEnumerable.Except方法仅输出listView3中的唯一数字,例如下图中listView1和listView2中的唯一数字是3和7.
I got 3 listViews 2 textbox and 2 buttons in WinForm.
Program Description: The program adds numbers to the listview by typing in numbers in the textbox and clicking the add button
Goal: I want to be able to use the IEnumerable.Except method to output only the unique numbers in listView3, for example in the picture below the 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 )
// {
// }
错误消息:System.Windows.Forms.ListView '不包含'Except'的定义,并且没有扩展方法'除了'接受类型为'System.Windows.Forms.ListView'的第一个参数'(您是否缺少using指令或汇编引用?)
链接到图片
https://picasaweb.google.com/102400252126244020492/IEnumerable#slideshow/6182752875853650498 [ ^ ]
推荐答案
尝试使用listview.Items
try using listview.Items
var nonintersect = listView1.Items.Except(listView2).Union(listView2.Items.Except(listView1));
将项目添加到列表3
adding items to list 3
listView3.Items.AddRange(nonintersect.Select(x=>(ListViewItem)x.Clone()).ToArray());
这篇关于在C#中使用IEnumerable除外的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!