从列表中获取对象的数量?

发布于 2024-10-20 19:11:23 字数 1251 浏览 1 评论 0原文

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* 属性获取 议程 的数量,

例如 Id = "1234567" 列表中有 3 个议程对象

Id = "3333333" has 3 Agenda object in List

Id = "2222222" has 2 Agenda object in List












enter code here
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 number 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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

枉心 2024-10-27 19:11:23
var groups = collection.GroupBy(x => x.Id);
foreach(var group in groups) {
    Console.WriteLine("Count for Id {0}: {1}", group.Key, group.Count());
}
var groups = collection.GroupBy(x => x.Id);
foreach(var group in groups) {
    Console.WriteLine("Count for Id {0}: {1}", group.Key, group.Count());
}
可爱咩 2024-10-27 19:11:23

(从 ViewModel.collection 中的 a.Id.Equals("1234567") 选择 a).Count();

(from a in ViewModel.collection where a.Id.Equals("1234567") select a).Count();

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文