如何在Linq中进行多对多的计数

  • 本文关键字:Linq linq many-to-many
  • 更新时间 :
  • 英文 :


所以我有

Business
Jobs (many per business)
JobApplications (many per job)

我只是想统计一下某项业务的申请总数。

但是我好像不能去。

Business.Jobs.JobApplications

如果我使用

Business.Jobs。SelectMany(l => l. jobapplications

可以从那里访问,但我不明白如何连接和计数

如何:

Business.Jobs.SelectMany(c => c.JobApplications).Count();

您必须替换一些名称。

bussiness
    .Select 
    (b => new 
        {
            id = b.id, 
            applications = b.jobs
                .Select (j => j.jobapplications.Count ()).Sum ()
        }
    )

以上方法适用于以下结构:

void Main()
{
    var bussiness = new List<Bussiness>()
    {
        new Bussiness
        {
            id = 1,
            jobs = new List<Jobs>()
            {
                new Jobs
                {
                    jobapplications = new List<JobApplications>()
                    {
                        new JobApplications(),
                        new JobApplications(),
                        new JobApplications(),
                        new JobApplications()
                    }
                },
                new Jobs
                {
                    jobapplications = new List<JobApplications>()
                    {
                        new JobApplications(),
                        new JobApplications(),
                        new JobApplications(),
                        new JobApplications()
                    }
                },
                new Jobs
                {
                    jobapplications = new List<JobApplications>()
                    {
                        new JobApplications(),
                        new JobApplications(),
                        new JobApplications(),
                        new JobApplications()
                    }
                }
            }
        },
        new Bussiness
        {
            id = 2,
            jobs = new List<Jobs>()
            {
                new Jobs
                {
                    jobapplications = new List<JobApplications>()
                    {
                        new JobApplications(),
                        new JobApplications(),
                        new JobApplications()
                    }
                },
                new Jobs
                {
                    jobapplications = new List<JobApplications>()
                    {
                        new JobApplications(),
                        new JobApplications(),
                        new JobApplications(),
                        new JobApplications(),
                        new JobApplications()
                    }
                },
                new Jobs
                {
                    jobapplications = new List<JobApplications>()
                    {
                        new JobApplications(),
                        new JobApplications(),
                        new JobApplications(),
                        new JobApplications(),
                        new JobApplications(),
                        new JobApplications()
                    }
                }
            }
        }   
    };
    //bussiness.Dump();
    bussiness.Select (b => new {id = b.id, applications = b.jobs.Select (j => j.jobapplications.Count ()).Sum ()})
    .Dump();
}
public class Bussiness
{
    public int id {get;set;}
    public IEnumerable<Jobs> jobs {get;set;}
}
public class Jobs
{
    public IEnumerable<JobApplications> jobapplications {get;set;}
}
public class JobApplications
{
}

你需要把它应用到你的结构中

相关内容

  • 没有找到相关文章

最新更新