如何从比较两个数组中检索不同的字符串? [英] How to retrieve distinct strings from comparing two arrays?

查看:86
本文介绍了如何从比较两个数组中检索不同的字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



使用列表框中的当前stud ID我想要在数组中检索缺席的学生姓名。



我在那里使用NOT IN条件但我是无法检索



然后我想用for循环来匹配得到ID不在列表框中的学生



但我仍然失去了价值而没有得到适当的缺席学生身份。





I want to retrieve Absent student names from sql database. 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.

Using the present stud id in list box i want to retrieve absent students name in an array.

I used NOT IN condition in where but i was not able to retrieve

Then i thought of using for loops to match the get the students who's id was not in the Listbox

But still I am losing the values and not getting proper absent students Id.


<pre>    private void checkabst_Click(object sender, EventArgs e)
    {
        string[] present = new string[3];
        string[] absent = new string[3];
        string[] allstd = new string[3];
        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());
                }
allstd=total.toArray();

              for(int i=0;i<listbox_present.Items.Count;i++)
            {
                present[i] = listbox_present.Items[i].ToString();

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

            }





过去几天就被困在这里。



我收到了礼物以及消息框中缺席的学生ID。



我尝试了什么:



我尝试使用Concat数组并删除重复但是也没有用。



Stuck here from past few days.

I am getting Present as well as the absent student Ids in messagebox.

What I have tried:

I tried it using Concat the arrays and removing duplicates but that too didnt work.

推荐答案

如果您的意思是

1)Studetails表包含学生的所有RFID ID号。

2)listbox_present包含出席的学生的RFID ID号。 />
3)你想找到表格中的所有RFID ID号,但不是ListBox。



然后你的代码开始很好: 总计应该包含所有学生

假设你的列表框是正确的 - 我无法分辨,我看不到它 - 然后呈现也是正确的(虽然我使用另一个List< T>而不是固定大小的数组)。

Th最简单的解决方案是循环现在,并调用List.Remove将每个在场的学生带出去。



在循环之后,你总共有所有缺少的学生。

这是非常简单的东西 - 只要你调用Remove方法 - 并且简单的foreach on present 会为你做的。



试一试:如果看起来不合适,请使用调试器检查总和出现,然后逐步查看正在发生的事情。
If you mean
1) The Studetails table contains all RFID id numbers for students.
2) The listbox_present contains the RFID id number for student who are present.
3) You want to find all the RFID id number that are in the table, but not the ListBox.

Then your code starts well: total should contain all the students
Assuming that your listbox is right - and I can't tell, I can't see it - then Present will be correct as well (though I'd use another List<T> instead of a fixed size array).
The simplest solution is to loop through present, and call List.Remove to take each present student out.

After the loop, you have all missing students in total.
That's pretty simple stuff - provided you call the Remove method - and simple foreach on present will do it for you.

Give it a try: if it doesn't look like it's working, use the debugger to check the contents of total and present before you enter the loop, then step through looking at what is happening.


这篇关于如何从比较两个数组中检索不同的字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆