rest时的关系?

我想在社区中添加一个人,我不确定它是如何完成的?

我的datacontract看起来像这样:

[DataContract(Name = "Community")] public class Community { public Group() { People = new List(); } [DataMember(Name = "CommunityName")] public string CommunityName { get; set; } public List People { get; set; } } [DataContract(Name = "Person")] public class Person { [DataMember(Name = "PersonName")] public string PersonName { get; set; } } 

在我的服务中,我可以像这样添加一个人或社区:

 List Communitys = new List(); List people = new List(); public void AddCommunity(Community community) { Communitys.Add(community); } public void AddPerson(Person person) { people.Add(person); } public void AddPersonToCommunity(Person person, Community community) { //not sure what to do here } 

但我不确定如何将一个人加入社区

 public void AddPersonToCommunity(Person person, Community community) { //community.CommunityName(Person.Add);? } 

我有一个Windows窗体,当我发布一个人或社区时,就像这样:

 { StringBuilder Community = new StringBuilder(); Community.AppendLine(""); Community.AppendLine("" + this.textBox4.Text + ""); Community.AppendLine(""); 

但是你如何(一旦完成)采取AddPersonToCommunity并创建一个字符串生成器,以便将一个人添加到社区?

  { StringBuilder CommunityAddPerson = new StringBuilder(); CommunityAddPerson.AppendLine(""); CommunityAddPerson.AppendLine ...... CommunityAddPerson.AppendLine("" + this.textBox4.Text + ""); CommunityAddPerson.AppendLine(""); 

希望这更清楚它的两个问题我猜。

我认为你的方法基于你所说的,可能看起来像这样:

 public void AddPersonToCommunity(string person, string communityName) { var result = communities.Where(n => String.Equals(n.CommunityName, communityName)).FirstOrDefault(); if (result != null) { result.Add(new Person() { Name = person }); } } 

您的POST数据可能如下所示:

  CrazyTown  Moe Howard   

您始终可以添加一个方法来将人员添加到社区:

 public void AddPersonToCommunity(Person person, Community community)... 

要么

 public void AddPersonToCommunity(string personName, string communityName)...