Get data with Sum from a table after multiple join

2019-09-12 04:14发布

问题:

I have the EDMX:

How can i get sum of Amount of single Freelancer from FreelancerPayment table if has? All time FreelancerPayment table doesn't has Amount if the freelancer is new. I just want list of freelancers with their details and Total earn. I tried this:

var freelancers = (from fl in db.FreelancerLogins
                           join f in db.Freelancers
                           on fl.FreelancerID equals f.FreelancerID
                           select new
                           {
                               FreelancerID = fl.FreelancerID,
                               UserName = fl.UserName,
                               EmailAddress = fl.EmailAddress,
                               EmailConfirmed = fl.EmailConfirmed,
                               Status = fl.Status,
                               LogInTime = fl.LogInTime,
                               LogOutTime = fl.LogOutTime,
                               Picture = f.Picture,
                               Title = f.Title,
                               Name = f.FirstName + " " + f.LastName,
                               Overview = f.Overview,
                               JoiningDate = f.JoiningDate,
                               BirthDay = f.BirthDay,
                               Rate = f.Rate,
                               Location = f.Location,
                               //Earn = (fp.Amount == null ? 0 : fp.Amount)
                               Earn=(
                               from fla in db.Freelancers
                           join apl in db.AppliedJobs
                           on fla.FreelancerID equals apl.FreelancerID into apll
                           from ap in apll.DefaultIfEmpty()

                           join jo in db.JobOffers
                           on ap.AppliedJobID equals jo.AppliedJobID into joo
                           from jobo in joo.DefaultIfEmpty()


                           join c in db.Contracts
                           on jobo.OfferID equals c.OfferID into coo
                           from con in coo.DefaultIfEmpty()

                           join fpay in db.FreelancerPayments
                           on con.ContractID equals fpay.ContractID into fpayy
                           from fp in fpayy.DefaultIfEmpty()

                                   select fp.Amount).Sum()


                           }).AsEnumerable();

        return freelancers.AsEnumerable();

But it returns :

Amount same for all. How can i put where clause for specific freelancer?

回答1:

Your inner query should start with AppliedJobs connected to the outer query Freelancer:

Earn = (
    from ap in db.AppliedJobs
    where f.FreelancerID == ap.FreelancerID

    join jo in db.JobOffers
    ...

Also you don't need left outer joins inside subquery that calculates the sum.

Finally, the way you are doing it makes it more complicated than it should be. You have nice navigation properties, so Don’t use Linq’s Join. Navigate! and you'll have no such issues:

var query = 
    from f in db.Freelancers
    let fl = f.FreelancerLogin
    select new
    {
        FreelancerID = fl.FreelancerID,
        UserName = fl.UserName,
        // ...
        Earn = (from ap in f.AppliedJobs
                from jo in ap.JofOffers
                from c in jo.Contracts
                let fp = c.FreelancerPayment
                select fp.Amount).DefaultIfEmpty().Sum()
    };