QuerySpec Grouping - Count

Posts   
 
    
nirataro
User
Posts: 5
Joined: 14-Jun-2006
# Posted on: 08-Dec-2013 14:16:54   

var q = qf
                .Create()
                .Select(() => new
                {
                    Id = JobOpportunityFields.Id.ToValue<int>(),
                    Title = JobOpportunityFields.Title.ToValue<string>(),
                    NumberOfApplicants =  xxx
                })
                .GroupBy(JobOpportunityFields.Id, JobOpportunityFields.Title)
                .From(qf.JobOpportunity.LeftJoin(qf.JobApplication).On(JobOpportunityFields.Id == JobApplicationFields.JobOpportunityId))
                .Where((JobOpportunityFields.EmployerId == employerId));
                

My goal is to generate this SQL code


select a.Id, a.Title, count(b.JobOpportunityId)
from JobOpportunity a left join JobApplication b
on a.Id = b.JobOpportunityId
where a.EmployerId = 2
group by a.Id, a.Title

So far I couldn't figure out how to fill the 'xxx' part with working statement. Function Functions.CountRow().ToValue<int>() does not take a field parameter.

nirataro
User
Posts: 5
Joined: 14-Jun-2006
# Posted on: 08-Dec-2013 14:28:16   

OK this works


    var field = JobApplicationFields.Id;
    field.AggregateFunctionToApply = SD.LLBLGen.Pro.ORMSupportClasses.AggregateFunction.Count;

            var q = qf
                .Create()
                .Select(() => new
                {
                    Id = JobOpportunityFields.Id.ToValue<int>(),
                    Title = JobOpportunityFields.Title.ToValue<string>(),
                    NumberOfApplicants = field.As("Total").ToValue<int>()
                })
                .GroupBy(JobOpportunityFields.Id, JobOpportunityFields.Title)
                .From(qf.JobOpportunity.LeftJoin(qf.JobApplication).On(JobOpportunityFields.Id == JobApplicationFields.JobOpportunityId))
                .Where((JobOpportunityFields.EmployerId == employerId));