如何从数组列表中检索最频繁的元素 [英] How to retrieve the most frequent element from an array list

查看:70
本文介绍了如何从数组列表中检索最频繁的元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

        mydb = new db();
        ArrayList list = new ArrayList();
        //List<datetime> list = new List<datetime>();
        string query1 = "select free_dates from I_mgr_free_dates where vacancy_id=" + "\'" + DropDownList1.SelectedItem + "\'";
        SqlDataReader read1 = mydb.getData(query1);
        while (read1.Read())
        {
            list.Add(Convert.ToDateTime(read1["free_dates"]));
        }

        mydb.CloseConnection();
        DateTime day1 = (DateTime)list[0];
        MessageBox.Show(day1.ToString());
</datetime></datetime>



我想从数组列表中检索最频繁的元素



i want to retrieve the most frequent element from the array list

推荐答案

在选择查询中使用order by desc,数组的第一个元素将是您最后输入的值.
Use order by desc in the select query and the first element of the array will be your latest entered value.


既然您还是要从数据库中获取它,就让它完成工作.擅长这种事情.查询是这样的(可能不太正确,我不是数据库专家):

Since you''re getting it from a database anyway, let it do the work. It''s good at this kind of thing. The query is something like this (may not quite be right, I''m not a database guy):

query = "select free_dates, count(free_dates) as number from I_mgr_free_dates where vacancy_id=" + "\''" + DropDownList1.SelectedItem + "\''" group by free_dates order by count desc limit 1";



请记住,如果该下拉列表包含用户输入的信息,从外部配置文件中提取的信息等,则需要对其进行转义以避免SQL注入攻击.尽可能使用参数化的查询.



Remember that if that dropdown list contains information entered by the user, pulled from an external configuration file etc you need to escape it to avoid SQL injection attacks. Use a parameterised query if possible.


频率是指出现特定项目的次数,或者表示最接近当前日期的日期?

如果是第一个,则必须设置一个计数器并增加每个匹配的日期,或将其插入匹配数组/列表中.如果是第二个,则必须比较日期.
Frequency as in the number of times a specific item appears or did you mean closest date to the current date?

If its the first then you''ll have to setup a counter and increment each date that is matched or insert it into your match array/list. If its the second then you''ll have to compare dates.


这篇关于如何从数组列表中检索最频繁的元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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