C#从列表中接收值

时间:2016-05-02 06:11:08

标签: c#

我正从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

4 个答案:

答案 0 :(得分:1)

如何更换

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

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

确定重复数据库端

答案 1 :(得分:1)

问题是代码执行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);

答案 2 :(得分:0)

试试这个

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

答案 3 :(得分:0)

我认为你需要稍微改变逻辑。试试这个......

navigator.camera.getPicture(
  cameraSuccess,
  cameraError,
    {
      destinationType: Camera.DestinationType.FILE_URI,
      sourceType: Camera.PictureSourceType.PHOTOLIBRARY,
    }
);