开发者

Get count of object from list?

开发者 https://www.devze.com 2023-02-15 05:37 出处:网络
public class Agenda { public string StartDate; public string EndDate; public string Id; } public Class Appointments
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();

0

精彩评论

暂无评论...
验证码 换一张
取 消