I have a class with the following properties:
public class Party
{
public string Type { get; set; }
public string Name { get; set; }
}
and this method to populate the list:
List<Party> parties = new List<Party>();
parties.Add(new Party {Type = "Plaintiff", Name = "Eilean Dover"});
parties.Add(new Party {Type = "Plaintiff", Name = "Bea O'Problem"});
parties.Add(new Party {Type = "Defendant", Name = "Anna Graham"});
parties.Add(new Party {Type = "Witness", Name = "John Doe"});
parties.Add(new Party {Type = "Witness", Name = "Rosa Shore"});
My goal is to put all the objects (parties) in a single string. So far, I have used String.Join and Linq in order to put them inline.
string partiesinline = string.Join(", ", parties.Select(x => String.Join(" ", new[] { x.Type, x.Name })));
After running this, the string partiesinline
looks like this:
Plaintiff Eilean Dover, Plaintiff Bea O'Problem, Defendant Anna Graham, Witness John Doe, Witness Rosa Shore
My futher improvement is to group the parties by their type and put the type in a plural form if there are more of the same type. Ideally, the string should look like this:
Plaintiffs Eilean Dover, Bea O'Problem, Defendant Anna Graham and Witnesses John Doe, Rosa Shore
I could be using a Dictionary or a List in order to get the plurar of the Type but I am stuck in grouping the parties and retaining in the partiesinline string a single type for the parties having the same name. Do you have any suggestions with explanation?
I have read Using LINQ to group a list of objects and Using Linq to group a list of objects into a new grouped list of list of objects but I am stuck, I am unable to identify the steps in order to get the desired result.