[英]How to retrieve Distinct string values from comparing two Arrays?
I want to retrieve Absent student names from sql database. 我想从sql数据库中检索缺席的学生姓名。 I have a Listbox in which i have Present student Rfid data (Something like 42 39 A0 11), I have stored the student details along with Rfid in database as nvarchar(1500) datatype.
我有一个列表框,其中有当前学生的Rfid数据(类似42 39 A0 11),我已将学生详细信息和Rfid一起存储为nvarchar(1500)数据类型。
Using the present stud id in list box i want to retrieve absent students name in an List. 我想使用列表框中的当前螺柱ID来检索列表中缺席的学生姓名。
Then i thought of using foreach loop to remove the students who's id was in the Listbox 然后我想到了使用foreach循环删除ID在列表框中的学生
But when i defined the two list like total and present with values and tried to remove the string from total which are in present the nthe output was successful 但是,当我定义两个列表(如total和存在值)并尝试从total中删除字符串(当前存在)时,输出成功
private void checkabst_Click(object sender, EventArgs e)
{
string[] present1 = new string[listbox_present.Items.Count];
List<string> present = new List<string> { ""};
List<string> absent = new List<string>();
List<string> total = new List<string>();
using (SqlConnection con = new SqlConnection(cs))
{
SqlCommand cmd = new SqlCommand("Select Rfid_Uid From Studetails", con);
con.Open();
SqlDataReader sdr = cmd.ExecuteReader();
while (sdr.Read())
{
total.Add(sdr[0].ToString());
}
}
present = listbox_present.Items.Cast<string>().ToList();
foreach(string temp in present)
{
total.Remove(temp);
}
foreach (string temp in total)
{
listbox_absent.Items.Add(temp);
}
}
Stuck here from past few days. 过去几天一直呆在这里。
The problem i think the listbox values are giving trouble while removing the string from total list. 我认为列表框值从总列表中删除字符串时出现问题。
You can achieve this simply by an Except
method. 您可以简单地通过
Except
方法来实现。 No need to for loops 无需循环
Remove following code 删除以下代码
bool isfound = false;
for (int i = 0; i < 3; i++)
{
isfound = false;
for (int j = 0; j < present.Length; j++)
{
if (allstd[i] == present[j])
{
isfound = true;
}
}
if (!isfound)
{
MessageBox.Show(allstd[i]);
}
}
and add 并添加
absent = allstd.Except(present).ToArray();
and iterate over these to show in MessageBox
. 并遍历这些内容以显示在
MessageBox
。
foreach (var stud in absent)
{
MessageBox.Show(stud);
}
Just to clarify after OP comment , Give a working example 只是为了在OP评论后澄清一下,举一个可行的例子
string[] present = { "A" , "C" , "D" ,"P"};
string[] absent = new string[3];
string[] allstd = { "A", "B", "C" ,"D" , "E" , "P"};
absent= allstd.Except(present).ToArray();
absent
will contain "B" and "E" absent
将包含“ B”和“ E”
Update after OP's latest code update. OP的最新代码更新后更新 。
Try replacing your method with following method with removed tow foreach loops
尝试用删除了两个
foreach loops
以下方法替换您的方法
private void checkabst_Click(object sender, EventArgs e)
{
string[] present1 = new string[listbox_present.Items.Count];
List<string> present = new List<string> { ""};
List<string> absent = new List<string>();
List<string> total = new List<string>();
using (SqlConnection con = new SqlConnection(cs))
{
SqlCommand cmd = new SqlCommand("Select Rfid_Uid From Studetails", con);
con.Open();
SqlDataReader sdr = cmd.ExecuteReader();
while (sdr.Read())
{
total.Add(sdr[0].ToString());
}
}
present = listbox_present.Items.Cast<string>().ToList();
absent = total.Except(present).ToArray();
foreach (var stud in absent)
{
MessageBox.Show(stud);
}
}
根据您在上述Ehsan回答中的评论,我不确定您是否知道:
System.Linq.Enumerable.Distinct
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.