如何检查列表中的项目数 [英] how to check for number of items in list

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

问题描述

我有一个从数据库填充的列表:



i have a list that is populated from database:

public List<string> listUserFirstName = new List<string>();

 while (reader.Read())
 {
     listUserFirstName.Add(reader["FirstName"].ToString().ToUpper());

     if (listUserFirstName.Capacity >= 2 )
     {
         msg = "Duplicate Names!";
     }
 }





我想查看重复的名称,即任何时候其内容超过一个, msg应该显示,上面的代码没有给我想要的结果。任何帮助将不胜感激。



I want to check for duplicate names, i.e. anytime its content is more than one, the msg should show, the above code doesnt give me the desired result. Any assistance would be appreciated.

推荐答案

public List<string> names = new List<string>();

while(reader.Read())
{
    string name = reader["FirstName"].ToString().ToUpper());
    if (!names.Contains(name))
        names.Add(names);
    else
    {
        // handle reporting the duplicate here
    }
}





如果列表很大,那么考虑使用Dictionary或HashSet而不是List来提高性能。



如果你只需要一个列表那些没有重复的名字



If the list is large, then consider using a Dictionary or a HashSet instead of a List to improve performance.

If all you need is a list of the names with no duplicates then

public List<string> names = new List<string>();

while(reader.Read())
{
    string name = reader["FirstName"].ToString().ToUpper());
    names.Add(names);
    names = names.Distinct().ToList();
}


这个或类似的东西应该适合:



This or something similar should suit:

public List<string> names = new List<string>();

while (reader.Read())
{
    string name = reader["FirstName"].ToString().ToUpper());
    if (names.Count == 0)
    {
        names.Add(name);
    }
    else
    {
        bool duplicate;

        foreach (string n in names)
        {
            if (n == name)
                duplicate = true;
        }

        if (!duplicate)
            names.Add(name);
    }
}





如果列表为空,那么你总是添加字符串,然后每次你如果列表中不存在,则只读取您添加的名称。这样你就永远不会产生一个带有重复的列表。



如果这不是你想要的那么我恐怕我误解了你的要求。



祝你好运。 :)



Here you are always adding the string if the list is empty, then each time you read a name you are only adding it if it does not exist in the list. This way you will never produce a list with duplicates in it.

If this is not what you want then I'm afraid I have misunderstood your request.

Good luck. :)


根据您的评论,这是一个更新的解决方案:



在命名空间中创建一个类型,类似于:



Based on your comment, here is an updated solution:

Create a type in your namespace, similar to this:

struct Record
{
    public string Name;
    public string PhoneNumber;
}





然后在你的方法中,比较类型值:





Then in your method, compare the type values:

public List<Record> Records = new List<Record>();

while (reader.Read())
{
    string name = reader["FirstName"].ToString().ToUpper());
    string phoneNumber = reader["PhoneNumber"].ToString();
    Record r = new Record();

    if (names.Count == 0)
    {
        r.Name = name;
        r.PhoneNumber = phoneNumber;
        Records.Add(r);
    }
    else
    {
        bool duplicate;

        foreach (Record record in Records)
        {
            if (record.Name == name &&
                record.PhoneNumber == phoneNumber)
                duplicate = true;
        }

        if (!duplicate)
            Records.Add(r);
        else
        {
            // Use whatever means you see fit here to
            // ask the user if they wish to replce the
            // existing record
        }
    }
}


这篇关于如何检查列表中的项目数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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