簡體   English   中英

C#從列表中接收值

[英]C# Receiving values from a list

我正在從列表中檢索一組值。 我的問題是我要檢索的項目重復。 即,我多次獲得同一項目的價值。

這是代碼。

            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

如何更換

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

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

確定重復的數據庫端

問題是代碼在每次讀取時都執行Distinct並保留打印邏輯,這不是您想要的。

將邏輯移出循環。

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);

嘗試這個

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;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM