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" } );
}
我希望Linq解决方案从 Viewmodel * Appointement *属性获取 Agendas 的数量
例如。 Id =“1234567”在列表中有3个议程对象
Id = "3333333" has 3 Agenda object in List
Id = "2222222" has 2 Agenda object in List
enter code here
答案 0 :(得分:1)
var groups = collection.GroupBy(x => x.Id);
foreach(var group in groups) {
Console.WriteLine("Count for Id {0}: {1}", group.Key, group.Count());
}
答案 1 :(得分:0)
(from a in ViewModel.collection where a.Id.Equals("1234567") select a).Count();