休息中的关系
本文关键字:关系 | 更新日期: 2023-09-27 18:30:10
我想在社区中添加一个人,但我不确定是怎么做到的?
我的数据合约是这样的:
[DataContract(Name = "Community")]
public class Community
{
public Group()
{
People = new List<Person>();
}
[DataMember(Name = "CommunityName")]
public string CommunityName { get; set; }
public List<Person> People { get; set; }
}
[DataContract(Name = "Person")]
public class Person
{
[DataMember(Name = "PersonName")]
public string PersonName { get; set; }
}
在我的服务中,我可以添加一个人或类似的社区:
List<Community> Communitys = new List<Community>();
List<Person> people = new List<Person>();
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>");
Community.AppendLine("<CommunityName>" + this.textBox4.Text + "</CommunityName>");
Community.AppendLine("</Community>");
但是(一旦完成)你将如何使用AddPersonToCommunity并创建一个字符串生成器来将一个人添加到社区中?
{
StringBuilder CommunityAddPerson = new StringBuilder();
CommunityAddPerson.AppendLine("<Community&{1}>");
CommunityAddPerson.AppendLine ......
CommunityAddPerson.AppendLine("<CommunityName>" + this.textBox4.Text + "</CommunityName>");
CommunityAddPerson.AppendLine("</Community>");
希望这是更清楚的两个问题在一个我想。
根据您所说的内容,我认为您的方法可能如下所示:
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数据可能如下所示:
<Community>
<CommunityName>CrazyTown</CommunityName>
<People>
<Person>Moe Howard</Person>
</People>
</Community>
您可以随时添加一种方法将人员添加到社区中:
public void AddPersonToCommunity(Person person, Community community)...
或
public void AddPersonToCommunity(string personName, string communityName)...