2012-04-06 92 views
4

我想添加一個人到社區,我不知道它是如何完成的?休息關係?

我datacontract看起來是這樣的:

[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; } 
} 

在我的服務,我可以添加一個人或一個社區,像這樣:

但我不確定如何加入一個人到社區

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

而且我有一個窗體窗體,當我發佈一個人或一個社區它是這樣完成的:

{ 
    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>"); 

希望這個問題更加明確,其中的一個問題我猜得很清楚。

+0

難道你不覺得你需要一個AddPersonToCommunity(社區社區,Person person)方法來完成你所說的話嗎? – 2012-04-06 16:23:29

+0

是的布賴恩,但我在這個方法中放什麼? – 2012-04-06 17:29:01

+0

你的問題和想法都在這個地方。請重新提出您的問題並更加連貫。目前還不清楚你在問什麼。 – 2012-04-06 18:43:13

回答

1

我覺得你的方法,根據你說的話,可能是這樣的:

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> 
+0

@KirstyWhite:您是指LINQ? – 2012-04-06 21:59:41

+0

@KirstyWhite:你可以從MSDN開始http://msdn.microsoft.com/en-us/library/bb397926.aspx – 2012-04-06 22:18:15

+0

@KirstyWhite:如果是這樣的話,你可能想回到一些C#的基礎知識。我不想在這裏創建一個討論。如果您遇到困難,請在此處張貼另一個問題。 – 2012-04-06 22:23:45

0

你總是可以添加一個方法來一個人添加到社區:

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

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