Click here to Skip to main content
15,886,087 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a list of students with their subject of interests (separated by comma).

C#
List<Students> students = new List<Students>();	
						   
students.Add(new Students() { Name = "Krishna", GroupID= 0, Interests = "Physics,Maths" });

students.Add(new Students() { Name = "Ganesh", GroupID= 0, Interests = "History,Physics" });

students.Add(new Students() { Name = "Jayesh", GroupID= 0, Interests = "Tech,Humanity" });

students.Add(new Students() { Name = "Aditya", GroupID= 0, Interests = "Science"});

students.Add(new Students() { Name = "Ramesh", GroupID= 0, Interests = "Programming,Science"});


I want to use the Linq to edit the GroupID of students in the same list based on their common interests.

Expected Output:

GroupID: 1
Students: Krishna, Ganesh
(Reason: Common interest in Physics)

GroupID: 2
Students: Aditya, Ramesh
(Reason: Common interest in Science)


Any help is greatly appreciated.

Thank you so much!

What I have tried:

Unfortunately, I'm not sure how to do this as I'm new to programming.
Posted
Updated 16-Aug-22 13:21pm
v2

1 solution

If you are new to programming, then the best advice I can give you with the problem as it stands is this: don't do it. Don't even try.

The problem is that you have made a big mistake in your DB / data design, and it's going to be a nightmare to sort out without some serious work on your part. And the mistake is that you are trying to store multiple pieces of information in a single item because that's "easy to do": storing the interests as a single string is easy to insert data, but a real PITA to process when you want to do anything complicated.

What you need to do is redesign your data, so you have a separate Interest class which stores the interest description as a separate string and a collection in your Student class which references individual Interest instances (think about a List<t> as a good start).

Then when you want to find common interests, it becomes a whole load easier: you can group students together by Interest instances and pick up the separate Student instances from that.

Think about it and you'll see what I mean.
 
Share this answer
 
Comments
PIEBALDconsult 16-Aug-22 19:25pm    
I'd start by making an enum with a Flags attribute for the interests. :D
OriginalGriff 17-Aug-22 1:22am    
Trouble with that is lack of flexibility - you have to recompile each time to add a new "possible interest". It was my first thought too, but ...
PIEBALDconsult 17-Aug-22 9:14am    
I know. But it's homework, so it's OK. :D
OriginalGriff 17-Aug-22 9:56am    
:D

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900