我如何在 Linq 中进行多对多计数
How can I do a count in Linq with 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
{
}
您需要将其应用于您的结构。
所以我有
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
{
}
您需要将其应用于您的结构。