C# 将项目添加到列表会导致重复项

C# Adding items to list causes duplicates

我想制作一个循环,将对象添加到列表中。每当我这样做时,它似乎一直在添加相同的对象。

private List<Events> GetEvents()
        {
            List<Events> dataEvents = new List<Events>();
            MySqlConnection con = new MySqlConnection(cs);
            con.Open();
            string sql = "SELECT * FROM Events";
            var cmd = new MySqlCommand(sql, con);
            MySqlDataReader rdr = cmd.ExecuteReader();

            while (rdr.Read())
            {
                Events x = new Events(rdr.GetInt32(0), rdr.GetString(1), rdr.GetInt32(2), rdr.GetInt32(3), rdr.GetDateTime(4));
                dataEvents.Add(x);
            }

            con.Close();
            return dataEvents;
        }

List<Events> newEvent = GetEvents();

这是我构建事件的方式 class:

public class Events
    {
        private static int _ID;
        private static string _Type;
        private static int _UserID;
        private static int _LokaalID;
        private static DateTime _Created_On;
        public Events(int id, string type, int UserID, int LokaalID, DateTime Created_On)
        {
            _ID = id;
            _Type = type;
            _UserID = UserID;
            _LokaalID = LokaalID;
            _Created_On = Created_On;
        }


    public int ID { get { return _ID; } }
    public string Type { get { return _Type; } }
    public int UserID { get { return _UserID; } }
    public int LokaalID { get { return _LokaalID; } }
    public DateTime Created_On { get { return _Created_On; } }

    }

Screenshot for objects in list or check below

这是我的数据库的样子,因此您可以看到它在我的数据库中没有重复:

Screenshot of database content

Laurent 做到了;永远不要使用 static,除非你确切地知道它的用途。让你的活动看起来像这样:

public class Events
{

    public Events(int id, string type, int UserID, int LokaalID, DateTime Created_On)
    {
        ID = id;
        Type = type;
        UserID = UserID;
        LokaalID = LokaalID;
        Created_On = Created_On;
    }


    public int ID { get; set; }
    public string Type { get; set; }
    public int UserID { get; set; }
    public int LokaalID  { get; set; }
    public DateTime Created_On  { get; set; }

}

但是一旦你解决了这个问题,试试这个;稍后您可能会出于不同的原因感谢我!

  • 打开项目的 NuGet 包管理器,

  • 搜索 Dapper

  • 安装它并将以下代码放入您的应用程序中:

        private List<Events> GetEvents()
        {
            using var con = new MySqlConnection(cs);
            return conn.Query<Event>("SELECT * FROM Events").ToList();
        }

是的..这就是您要做的所有事情*; Dapper 将创建对象、open/close 连接、读取数据、将其填充到对象中等。

*好吧,除非您的 C# 属性 名称与列名称不一致,在这种情况下,请使用 AS 重命名您的列,以便它们 do 匹配:

//assume the column is called EventId and the C# prop is ID
conn.Query<Event>("SELECT EventId as ID, ... FROM Events").ToList()

要进行参数化查询:

conn.Query<Event>("SELECT * FROM Events WHERE ID = @someId", new { someId = 123 }).ToList()

它的工作量更大,但这应该可以帮助您入门,这意味着您不必花费数小时来编写人类已知的最乏味的 GetInt, GetString, Get.. Get.. 代码..

您的事件 class 具有静态的私有字段,因此这些字段的值对于您的所有事件实例都是通用的……

这是正确的代码。它可以简化,具体取决于您使用的 .NET 版本。

public class Events
{
    private int _ID;
    private string _Type;
    private int _UserID;
    private int _LokaalID;
    private DateTime _Created_On;
    public Events(int id, string type, int UserID, int LokaalID, DateTime Created_On)
    {
        _ID = id;
        _Type = type;
        _UserID = UserID;
        _LokaalID = LokaalID;
        _Created_On = Created_On;
    }


    public int ID { get { return _ID; } }
    public string Type { get { return _Type; } }
    public int UserID { get { return _UserID; } }
    public int LokaalID { get { return _LokaalID; } }
    public DateTime Created_On { get { return _Created_On; } }
}

编写此代码的现代方式 (C# 9) 是

public class Events
{
    public int ID { get; init; }
    public string Type { get; init; }
    public int UserID { get; init; }
    public int LokaalID { get; init; }
    public DateTime Created_On { get; init; }
}

旁注:为什么将您的 class 事件称为一个事件?

从您的字段中删除静态修饰符:

public class Events
{
    private int _ID;
    private string _Type;
    private int _UserID;
    private int _LokaalID;
    private DateTime _Created_On;

    public Events(int id, string type, int UserID, int LokaalID, DateTime Created_On)
    {
        _ID = id;
        _Type = type;
        _UserID = UserID;
        _LokaalID = LokaalID;
        _Created_On = Created_On;
    }


    public int ID { get { return _ID; } }
    public string Type { get { return _Type; } }
    public int UserID { get { return _UserID; } }
    public int LokaalID { get { return _LokaalID; } }
    public DateTime Created_On { get { return _Created_On; } }
}