所以我有一个 Windows 窗体应用程序(客户端),它显示哪些组属于学生。
在服务端,这样做是这样的:
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;
}
要将学生添加到组中,我使用了以下方法:
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 });
}
}
我的 GET 方法如下所示:
[OperationContract]
[WebInvoke(Method = "GET", BodyStyle = WebMessageBodyStyle.Bare, RequestFormat = WebMessageFormat.Xml, ResponseFormat = WebMessageFormat.Xml, UriTemplate = "/GetStudentCollectionByGroup/{anything}")]
List<Group> GetStudentCollectionByGroup(string anything);
数据合约如下所示:
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; }
}
现在,当我输入特定的学生时,我从客户端获取组没有问题。但是,当我输入不同的文本框以根据 Group 获取学生时,什么都没有返回?例如,如果我键入组“A”,它应该返回属于该组的所有学生。就像我如何返回属于学生“B”的一组组