So I have an windows form application (client) which displays which groups belong to a student.
On the service side this is done like so:
List<Student> students = new List<Student>();
List<Group> Groups = new List<Group>();
public List<Group> GetStudentCollectionByGroup(string anything)
{
List<Group> groups = (from g in Groups
where
(from t in g.Groupsz where
string.Equals(t.StudentID, anything, StringComparison.CurrentCultureIgnoreCase)
|| string.Equals(t.FirstName, anything, StringComparison.CurrentCultureIgnoreCase)
|| string.Equals(t.LastName, anything, StringComparison.CurrentCultureIgnoreCase)
select t).Count() > 0
select g).ToList();
return groups;
}
To add a student to a group I have used this method:
public void AddStudentToGroup(string group, string studentID, string firstName, string lastName)
{
var result = Groups.Where(n => String.Equals(n.GroupName, group)).FirstOrDefault();
if (result != null)
{
result.Groupsz.Add(new Student() { StudentID = studentID });
result.Groupsz.Add(new Student() { FirstName = firstName });
result.Groupsz.Add(new Student() { LastName = lastName });
}
}
And my GET method looks like this:
[OperationContract]
[WebInvoke(Method = "GET", BodyStyle = WebMessageBodyStyle.Bare, RequestFormat = WebMessageFormat.Xml, ResponseFormat = WebMessageFormat.Xml, UriTemplate = "/GetStudentCollectionByGroup/{anything}")]
List<Group> GetStudentCollectionByGroup(string anything);
The data contracts look like this:
public class Student
{
public Student()
{
StudentGroup = new List<Group>();
}
[DataMember(Name = "StudentID")]
public string StudentID { get; set; }
[DataMember(Name = "FirstName")]
public string FirstName { get; set; }
[DataMember(Name = "LastName")]
public string LastName { get; set; }
public List<Group> StudentGroup { get; set; }
}
[DataContract(Name = "Group")]
public class Group
{
public Group()
{
Groupsz = new List<Student>();
}
public string GroupName { get; set; }
public List<Student> Groupsz { get; set; }
}
Now I have no problem getting the group from my client side when I type in the specific student. But when I type into a different textbox to get the students based on Group nothing is returned? For instance if I type group “A” it should return all the students who belong to that group. Just like how I return a collection of groups who belong to student “B”
I think I found the problem, not sure why but adding the student to the group and group to the student seems to work. However I could be duplicating records.