LINQ:我可以从集合中列表中的对象获取属性吗?

发布于 2024-12-04 06:24:57 字数 713 浏览 0 评论 0原文

    foreach (ReportType t in reportsCollection)
    {
        List<Report> reps = (from r in t.reports where r.isChecked == true select r).ToList<Report>();
        foreach (Report r in reps)
            System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo(r.path));
    }

这个声明效果很好。我只是在寻找一个可以做到这一点的 LINQ 语句。

基本上,我有一个带有名为 isChecked 属性的报告对象。 reportsCollection 包含多个reportTypes,其中包含该类型的报告列表。所以集合看起来像这样:

type1

  • 报告
  • 报告 2
  • 报告 3

类型 2

  • 报告 4
  • 报告 5
  • 报告 6

等等。

我想要一个 LINQ 语句来获取这些类型中 isChecked == true 的所有报告。我怀疑循环是必要的,但我很好奇社区是否有解决方案。谢谢!

    foreach (ReportType t in reportsCollection)
    {
        List<Report> reps = (from r in t.reports where r.isChecked == true select r).ToList<Report>();
        foreach (Report r in reps)
            System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo(r.path));
    }

This statement works just fine. I'm just looking for a single LINQ statement that could maybe do this.

Basically, I have a report object with a property called isChecked. reportsCollection contains several reportTypes that contains lists of reports of that type. So the collection looks like this:

type1

  • report
  • report 2
  • report 3

type 2

  • report 4
  • report 5
  • report 6

and so on.

I want a LINQ statement that will grab all reports where isChecked == true within those types. I suspect a loop is necessary, but I was curious to see if the community had a solution. Thanks!

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

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

发布评论

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

评论(2

暮光沉寂 2024-12-11 06:24:57

您想使用 SelectMany

var query = reportsCollection.SelectMany(t => t.reports)
                             .Where(r => r.isChecked == true)
                             .ToList();

查询表达式语法形式,您可以将其写为

var query = (from type in reportsCollection
            from report in type.reports 
            where report.isChecked == true
            select report).ToList();

You want to use SelectMany

var query = reportsCollection.SelectMany(t => t.reports)
                             .Where(r => r.isChecked == true)
                             .ToList();

In query expression syntax form, you might write it as

var query = (from type in reportsCollection
            from report in type.reports 
            where report.isChecked == true
            select report).ToList();
王权女流氓 2024-12-11 06:24:57

在查询语法中,它看起来像这样

var query = from t in reportCollection
            from r in t.reports 
            where r.isChecked == true
            select r;

In query syntax it will look like this

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