C# 无法将类型WebApplication1.Add.ClassName[]隐式转换为int[]

C# 无法将类型WebApplication1.Add.ClassName[]隐式转换为int[],c#,C#,这是我想在数组中保存AllRecords或Notes的代码,但我已经创建了另一个类并声明了列的属性,这是代码 public class ClassName { //public int Col1 { get; set; } public int Col2 { get; set; } } public static void countCurrency(int amount) { SqlConnection con = new SqlConnection("Se

这是我想在数组中保存AllRecords或Notes的代码,但我已经创建了另一个类并声明了列的属性,这是代码

public class ClassName
{
    //public int Col1 { get; set; }
    public int Col2 { get; set; }
}

public static void countCurrency(int amount)
{
    SqlConnection con = new SqlConnection("Server=DESKTOP-8QL52AL\\ASADI; Database=atm;  Integrated Security=True;");

    ClassName[] allRecords = null;
    string sql = @"SELECT id,notes
       FROM  notes";
    using (var command = new SqlCommand(sql, con))
    {
        con.Open();
        using (var reader = command.ExecuteReader())
        {
            var notes = new List<ClassName>();
            while (reader.Read())
                notes.Add(new ClassName { Col2 = reader.GetInt32(1) });
            allRecords = notes.ToArray();

            int asad = notes.Count;
            int[] noteCounter = new int[asad];
            int[] arra = allRecords.ToArray();
            //int[] noteCounters = allRecords;
这里它给了我这个错误:

无法将类型WebApplication1.Add.ClassName[]隐式转换为int[]

这就是为什么我不能在下面的if循环中应用这个操作数>=

for (int i = 0; i < notes.Count; i++)
{
    if (amount >= notes[i])
    {
        noteCounter[i] = amount / notes[i];
        amount = amount - noteCounter[i] * notes[i];
    }
}

如果要将Col2属性中的值分配给int数组,可以使用linq

int[] arra = allRecords.Select(c => c.Col2).ToArray();
在for循环中,可以通过将amount>=notes[i]替换为amount>=notes[i].Col2来进行所需的比较


我还建议重命名Col2属性,给它一个描述其值实际表示的名称。

数据库的列索引1中的内容不是int。它可能是null或无法转换为整数的字符串。您想做什么?您想要一个int数组,还是一个ClassName数组?int数组@TomW@jdweng它是数据库中的整数,我已检查它是否来自数据库是否有空值?数据库将允许单元格为null,而c需要使用int显式允许null?您的意思是c=>c.Col2吗?c->c.Col2不会在c中编译。我编辑了您的问题以使代码编译并删除了否决票。请确保您的代码在发布前已编译。