比较2序列IEnumerable.Except在C# [英] Compare 2 sequences IEnumerable.Except in C#
问题描述
我想比较这两个序列之间的不同使用 IEnumerable.Except
。
I want to compare how these two sequences are different using IEnumerable.Except
.
程序说明:: 该方案是一个抛硬币游戏。 1头,2是尾巴。
Program Description:: The program is a Coin Toss Game. 1 is heads, 2 is tails.
该程序循环5次,每次循环的程序检查,如果数字匹配的时间。数字是1或2我用随机的。如果硬币匹配它更新到列表视图。
The program loops 5 times, each time it loops the program checks if the numbers matched. Numbers are 1 or 2 I used Random. If the coin matches it updates it to the listView.
标签:( lbl_Numb_1
, lbl_Numb_2
, lbl_Numb_3
, lbl_Numb_4
)生成随机数,1或2。 lbl_Result_1
拷贝数 lbl_Numb_2
并将它们添加到ListView1的。 lbl_Reslt_2
复制 lbl_Numb_4
键,将其添加到listview2。
Label: (lbl_Numb_1
, lbl_Numb_2
, lbl_Numb_3
, lbl_Numb_4
) generate random numbers, either 1 or 2. lbl_Result_1
copies the number in lbl_Numb_2
and adds them to the listview1. lbl_Reslt_2
copies lbl_Numb_4
and adds it to the listview2.
我已经尝试过自己,但我似乎不能将它得到它。是否有人可以帮助我。
I've tried it myself, but I can't seem it get it. Can someone please help me out.
private void btn_Match_Click(object sender, EventArgs e)
{
//.Next means so that random number is not negative
lbl_Numb_1.Text = rand.Next(1, 3).ToString();
lbl_Numb_2.Text = rand.Next(1, 3).ToString();
//Random Numbers for listbox2
lbl_Numb_3.Text = rand.Next(1, 3).ToString();
lbl_Numb_4.Text = rand.Next(1, 3).ToString();
//This is for the first listbox.
if (lbl_Numb_1.Text == lbl_Numb_2.Text)
{
lbl_Result_1.Text = lbl_Numb_2.Text;
//i = shows sequence id
ListViewItem lvi = new ListViewItem("" + i);
lvi.SubItems.Add(lbl_Result_1.Text);
listView1.Items.Add(lvi);
}
//This is for the secound listbox
if (lbl_Numb_3.Text == lbl_Numb_4.Text)
{
lbl_Result_2.Text = lbl_Numb_4.Text;
//i = shows sequence id
ListViewItem lview = new ListViewItem("" + i);
lview.SubItems.Add(lbl_Result_2.Text);
listView2.Items.Add(lview);
}
}
推荐答案
在使用IEnumerable.Except(),你必须要小心。因为从你比较的基础上的结果不同。 例如:
While using IEnumerable.Except() you need to be careful. Because the result differs based on from what you compare. Example:
List<int> l1 = new List<int>();
l1.Add(1);
l1.Add(2);
l1.Add(3);
List<int> l2 = new List<int>();
l2.Add(2);
l2.Add(4);
IEnumerable<int> l3 = l2.Except(l1); // l3 will have 4
IEnumerable<int> l4 = l1.Except(l2); //l4 will have 1,3
你可以做的是使用IEnumberable.Except两种方式,并将其存储在一个单一的列表。
What you can do is use IEnumberable.Except in both ways and store it in a single List.
IEnumerable<int> e1 = l2.Except(l1);
IEnumerable<int> e2 = l1.Except(l2);
List<int> l3 = new List<int>();
foreach (var item in e1)
{
l3.Add(item);
}
foreach (var item in e2)
{
l3.Add(item);
}
13将举行哪些不是present在这两个列表中的项目。
l3 will hold items which are not present in both the lists.
这篇关于比较2序列IEnumerable.Except在C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!