C#从列表中接收值

  • 本文关键字:列表 c#
  • 更新时间 :
  • 英文 :


我正在从列表中检索一组值。我的问题是我正在检索的项目有重复。即,我不止一次获得同一物品的价值。

这是代码。

            string test = ""; ;
            con.Open();
            SqlCommand get = new SqlCommand("select * from Color_Subjects", con);
            SqlDataReader read = get.ExecuteReader();
            List<Subject> subjects = new List<Subject>(); // Declare a list of subjects
            while (read.Read())
            {
                subjects.Add(new Subject()
                {
                    SubjectName = read.GetString(0),
                    Color = ((int)read.GetValue(1))
                });
                //Get All Unique Colors
                List<int> allColors = subjects.Select(x => x.Color).Distinct().ToList();
                //Iterate through each color and get subjects associated with that color
                foreach (int thisColor in allColors)
                {
                    List<Subject> subjectsForThisColor = subjects.Where(x => x.Color == thisColor).ToList();
                    // Output to console -- 
                    foreach (Subject s in subjectsForThisColor)
                    {
                        //Console.WriteLine(s.SubjectName + " - " + s.Color);
                        test += s.SubjectName + " -" + s.Color + "n";
                    }
                }
            }
            TextBox7.Text = test;

观察输出:

C#   -1
C#   -1
SSM  -2
C#   -1
SSM  -2
OOMD     -3
C#   -1
SSM  -2
OOMD     -3
MMT  -4
C#   -1
SSM  -2
OOMD     -3
MMT  -4
Elective-1   -5
C#   -1
SSM  -2
OOMD     -3
MMT  -4
Elective-1   -5
Elective-2   -6
C#   -1
SSM  -2
OOMD     -3
MMT  -4
Elective-1   -5
Elective-2   -6
Elective-3   -7
C#   -1
SSM  -2
OOMD     -3
MMT  -4
Elective-1   -5
Elective-2   -6
Elective-3   -7
Elective-4   -8
C#   -1
SSM  -2
OOMD     -3
MMT  -4
Elective-1   -5
Elective-2   -6
Elective-5   -6
Elective-3   -7
Elective-4   -8
C#   -1
SSM  -2
OOMD     -3
MMT  -4
Elective-1   -5
Elective-2   -6
Elective-5   -6
Elective-3   -7
Elective-4   -8
Elective-6   -8

所需输出:

    C#           -1
    SSM          -2
    OOMD         -3
    MMT          -4
    Elective-1   -5
    Elective-2   -6
    Elective-5   -6
    Elective-3   -7
    Elective-4   -8
    Elective-6   -8

问题是代码在每次读取时执行Distinct和rest打印逻辑,这不是您想要的。

将逻辑移到循环之外。

while (read.Read()) 
{
    subjects.Add(new Subject() 
    {
        SubjectName = read.GetString(0),
        Color = ((int) read.GetValue(1))
    });
}
//Get All Unique Colors
List < int > allColors = subjects.Select(x => x.Color).Distinct().ToList();
//Iterate through each color and get subjects associated with that color
foreach(int thisColor in allColors) 
{
    List < Subject > subjectsForThisColor = subjects.Where(x => x.Color == thisColor).ToList();
    // Output to console -- 
    foreach(Subject s in subjectsForThisColor) 
    {
        //Console.WriteLine(s.SubjectName + " - " + s.Color);
        test += s.SubjectName + " -" + s.Color + "n";
    }
}

此外,如果您正在寻找格式化输出的方法,请尝试探索Composite Formatting选项并执行类似操作。

Console.WriteLine("{0,-10}-{1}", s.SubjectName,s.Color);

更换怎么样

SqlCommand get = new SqlCommand("select * from Color_Subjects", con);

带有

SqlCommand get = new SqlCommand("select DISTINCT * from Color_Subjects", con);

以确定重复的数据库端

尝试这个

List<Subject> subjectsForThisColor = subjects.Where(x => x.Color == thisColor).Select(c=>c).Distinct().ToList();

我认为您需要稍微更改一下逻辑。。试试这个。。。

        string test = ""; ;
        con.Open();
        SqlCommand get = new SqlCommand("select * from Color_Subjects", con);
        SqlDataReader read = get.ExecuteReader();
        List<Subject> subjects = new List<Subject>(); // Declare a list of subjects
        while (read.Read())
        {
            subjects.Add(new Subject()
            {
                SubjectName = read.GetString(0),
                Color = ((int)read.GetValue(1))
            });
        }
            //Get All Unique Colors
        List<int> allColors = subjects.Select(x => x.Color).Distinct().ToList();
        //Iterate through each color and get subjects associated with that color
        foreach (int thisColor in allColors)
        {
            List<Subject> subjectsForThisColor = subjects.Where(x => x.Color == thisColor).ToList();
            // Output to console -- 
            foreach (Subject s in subjectsForThisColor)
            {
                //Console.WriteLine(s.SubjectName + " - " + s.Color);
                test += s.SubjectName + " -" + s.Color + "n";
            }
        }
        TextBox7.Text = test;

最新更新