如何在Linq中进行多次计算

时间:2015-05-27 09:48:37

标签: linq many-to-many

所以我有

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

我只想计算一个企业的应用程序总数。

但我似乎无法去

Business.Jobs.JobApplications

如果我使用

Business.Jobs.SelectMany(l => l.JobApplications

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

2 个答案:

答案 0 :(得分:2)

怎么样:

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

答案 1 :(得分:1)

你必须更换一些名字。

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
{

}

您需要将其应用于您的结构。