I was getting help related to a previous question but then told to ask a new question related to it but the code given I run into an error:
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() { PersonName = person }); //no definition for add?
}
}
You can see the previous question here for more specifics: relationships in rest?
If I do var result = communities; result will then have the definition for Add so im not sure whats going on?
You’re calling
Where()which will return anIEnumerable<Community>(noAddmethod) and thenFirstOrDefault()which returns aCommunity(also noAddmethod). Where would you expect anAddmethod to come from?I suspect you really want:
… because
Community.Peopleis a list of the people in that community, right?Note that if you do
var result = communities;there will indeed be an Add method – but with a signature ofAdd(Community), notAdd(Person).It’s important that you keep the types of everything straight. This actually has very little to do with LINQ. You’d have seen the same result if you’d tried: