简体   繁体   English

如何从此C#代码中找出结果

[英]How do I find out the result from this C# code

//10 objects will be defined here
 Students s1 = new Students{};
 s1.id = 101;
 s1.name= "Andy";
 s1.subject = "BIS101";
 s1.score = 89;

 Students s2 = new Students{};
 s2.id = 101;
 s2.name= "Andy";
 s2.subject = "BIS102";
 s2.score = 95;

 Students s3 = new Students{};
 s3.id = 102;
 s3.name= "Katty";
 s3.subject = "BIS103";
 s3.score = 70;

 Students s4 = new Students{};
 s4.id = 103;
 s4.name= "Aimy";
 s4.subject = "BIS101";
 s4.score = 70;

 Students s5 = new Students{};
 s5.id = 104;
 s5.name= "Kody";
 s5.subject = "BIS102";
 s5.score = 60;

 Students s6 = new Students{};
 s6.id = 104;
 s6.name= "Kody";
 s6.subject = "BIS103";
 s6.score = 70;

 Students s7 = new Students{};
 s7.id = 103;
 s7.name= "Aimy";
 s7.subject = "BIS103";
 s7.score = 50;

 Students s8 = new Students{};
 s8.id = 102;
 s8.name= "Kathy";
 s8.subject = "BIS102";
 s8.score = 40;

 Students s9 = new Students{};
 s9.id = 105;
 s9.name= "Pretty";
 s9.subject = "BIS103";
 s9.score = 50;

 Students s10 = new Students{};
 s10.id = 105;
 s10.name= "Pretty";
 s10.subject = "BIS101";
 s10.score = 60;

         //create a new basket
 List<Students> list1 = new List<Students>();

 //add those objects into basket
 list1.Add(s1);list1.Add(s2);list1.Add(s3);list1.Add(s4);
 list1.Add(s5);list1.Add(s6);list1.Add(s7);list1.Add(s8);
 list1.Add(s9);list1.Add(s10);

From this code, how do I find out students who have enrolled for two courses?? 从此代码中,我如何找出已报读两门课程的学生? I have been trying to sort this out and I am a newbie so I couldn't get any further. 我一直在努力解决这个问题,我是新手,所以我再也无法掌握了。

Any help would be very much appreciated! 任何帮助将不胜感激!

Thanks in advance! 提前致谢!

Here's a fairly simple LINQ query to find what you want: 这是一个相当简单的LINQ查询,以查找您想要的内容:

list1.GroupBy(s => s.id).Where(s => s.Count() == 2);

Or, you could get there by looping: 或者,您可以通过循环到达那里:

var dict = new Dictionary<int, int>();
var matches = new List<int>();

foreach(var s in list1)
{
    if(dict.ContainsKey(s.id)) dict[s.id] = dict[s.id] + 1;
    else dict.Add(s.id, 1);
}

foreach(var kvp in dict)
{
    if(kvp.Value == 2) matches.Add(kvp.Key);
}

Keep in mind, though, that if two Student instances share an Id, they're really the same student. 但是请记住,如果两个Student实例共享一个ID,则它们实际上是同一学生。 If that occurs, you may want to re-think your class structure. 如果发生这种情况,您可能需要重新考虑您的类结构。 I would suggest something like: 我建议类似的东西:

public class Course
{
    public string Name { get; set; }
    public int Score { get; set; }
}

public class Student
{
    public int Id { get; set; }
    public string Name { get; set; }
    public List<Course> Courses { get; set; }
}

Which means that for any student, you would simply have to: 这意味着对于任何学生来说,您只需要:

list1.Where(s => s.Courses.Length == 2);

You can use Linq for this: 您可以为此使用Linq:

list1.GroupBy(s => s.id).Where(g => g.Count() == 2);

You should also consider redoing your class abstraction so that the Student object holds a list of courses. 您还应该考虑重做您的类抽象,以便Student对象保存课程列表。 Your abstraction right now is confusing, since multiple instances of the Student class actually represent the same student. 您现在的抽象令人困惑,因为Student类的多个实例实际上代表同一位学生。

I also recommend that you look at object and collection initialization syntax , which would help clean up this code a lot. 我还建议您查看对象和集合初始化语法 ,这将大大帮助清理此代码。

With the new abstraction and use of the initialization syntax, your instantation could look like: 通过新的抽象和初始化语法的使用,您的实例可能类似于:

var student1 = new Student { ID = 10, Courses = new List<Course> { c1, c2 } };
var students = new List<Student> { student1, student2 };

And then your Linq query would be: 然后您的Linq查询将是:

students.Where(s => s.Courses.Count() == 2);

You have 2 ways of getting to the answer: 您有2种获得答案的方法:

First you have to understand how to do what you need 首先,您必须了解如何做所需的事情

You need to know students with at least 2 courses. 您需要了解至少有2门课程的学生。
What you have is 10 Student objects that can refer to the same student but different course. 您拥有的是10个Student对象,它们可以引用相同的学生但课程不同。
How you define the same student is in how many courses: you need to start counting that a single student have a Student Object with same ID or Name to identify it is the same Student and having a different course property; 如何定义同一学生有多少门课程:您需要开始计算一个学生的学生对象具有相同的ID或名称,以识别该学生是同一学生并且具有不同的课程属性; you can make a cycle for each of the Student objects. 您可以为每个Student对象建立一个循环。

Then have a "checklist": if you have not seen the name, then you add him to the list and count 1 course; 然后有一个“清单”:如果您还没有看到名字,则将他添加到列表中并计算1门课程; if the name is different, then you add another name to the list and add 1 course to that name; 如果名称不同,则将另一个名称添加到列表中,并在该名称上添加1条路线; if the name is already in your list, then you add 1 course to the name you are checking. 如果名称已经在您的列表中,则将1门课程添加到要检查的名称中。 After the 10 students your checklist will have Andy 2, Katty 2, Aimy 2, Kody 2, Pretty 2; 在这10名学生之后,您的清单将包括Andy 2,Katty 2,Aimy 2,Kody 2,Pretty 2; then your you need to check your checklist and see for every element that if it has 2 or more courses to match your criteria. 那么您需要检查清单,并查看是否有2门或更多门课程符合您的条件的每个元素。

When programming, try to separate your problem, see the what you have, what you need, and try to separate each step between as much as you can; 进行编程时,请尝试分离问题,查看所拥有的,所需要的,并尽可能地将每个步骤分开。 these are called algorithms. 这些称为算法。

You can start with how you understand and then advance to best code options like, the last answers like Justin Niessner's (advanced answer): 您可以从如何理解开始,然后进入最佳代码选项,例如贾斯汀·尼斯纳(Justin Niessner)的最后答案(高级答案):

list1.GroupBy(s => s.id).Where(s => s.Count() > 1);

You have your list (every student), it will be grouped by ids, so every element that have the same id will be "together", Where refers to a condition of what you need, the s.count refers to the elements that are together like "Pretty" that has the subject "BIS103" and "BIS101", the count of the elements is 2, so it match the Where(2 > 1) criteria 您有自己的列表(每个学生),它将按ID分组,因此具有相同ID的每个元素都将“在一起”, Where您需要的条件,而s.count则指的是像主题为“ BIS103”和“ BIS101”的“ Pretty”一样,元素的数量为2,因此它与Where(2 > 1)条件匹配

As you said you were a newbie I tried to explain it as I remember from school. 正如您所说的,您是个新手,我记得在学校时就试图解释它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM