public class Agenda
{
public string StartDate;
public string EndDate;
public string Id;
}
public Class Appointments
{
public Agenda agenda;
}
public class ViewModel
{
List<Appointments> collection = new List<Appointments>();
collection.Add(new Agenda { StartDate = "date1" , EndDate = "date1", Id= "1234567" } );
collection.Add(new Agenda { StartDate = "date2" , EndDate = "date2", Id= "1234567" } );
collection.Add(new Agenda { StartDate = "date3" , EndDate = "date3", Id= "2222222" } );
collection.Add(new Agenda { StartDate = "date4" , EndDate = "date4", Id= "3333333" } );
collection.Add(new Agenda { StartDate = "date5" , EndDate = "date5", Id= "3333333" } );
collection.Add(new Agenda { StartDate = "date1" , EndDate = "date1", Id= "3333333" } );
collection.Add(new Agenda { StartDate = "date1" , EndDate = "date1", Id= "1234567" } );
}
I want Linq solution to get the num开发者_如何学JAVAber of Agendas from the Viewmodel *Appointement* property
eg. Id = "1234567" has 3 Agenda object in List
Id = "3333333" has 3 Agenda object in List
Id = "2222222" has 2 Agenda object in List
enter code here
var groups = collection.GroupBy(x => x.Id);
foreach(var group in groups) {
Console.WriteLine("Count for Id {0}: {1}", group.Key, group.Count());
}
(from a in ViewModel.collection where a.Id.Equals("1234567") select a).Count();
精彩评论