繁体   English   中英

如何通过比较两个数组来检索不同的字符串值?

[英]How to retrieve Distinct string values from comparing two Arrays?

我想从sql数据库中检索缺席的学生姓名。 我有一个列表框,其中有当前学生的Rfid数据(类似42 39 A0 11),我已将学生详细信息和Rfid一起存储为nvarchar(1500)数据类型。

我想使用列表框中的当前螺柱ID来检索列表中缺席的学生姓名。

然后我想到了使用foreach循环删除ID在列表框中的学生

但是,当我定义两个列表(如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);
            }
        }

过去几天一直呆在这里。

我认为列表框值从总列表中删除字符串时出现问题。

您可以简单地通过Except方法来实现。 无需循环

删除以下代码

 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]);
            }

        }

并添加

absent = allstd.Except(present).ToArray();

并遍历这些内容以显示在MessageBox

 foreach (var stud in absent)
        {
             MessageBox.Show(stud);
        }

只是为了在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将包含“ B”“ E”

OP的最新代码更新后更新

尝试用删除了两个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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM