Sub Query /Getting SUM Using LINQ

Last post 06-21-2009 11:41 PM by malcolms. 11 replies.

Sort Posts:

  • Sub Query /Getting SUM Using LINQ

    06-21-2009, 5:41 PM
    • Member
      12 point Member
    • learndontnet
    • Member since 03-05-2009, 3:51 AM
    • Posts 36

    Hi All,

    Hope you guys are doing well. Here I have an issue with my LINQ query basically I need to get the sum of a column and some other columns values and for that the SQL query would be like this.

     

    select pr.Name, pr.Description, (select SUM(f.TotalAmount) from  Project p
    inner join  ProjectFund pf on p.ProjectID = pf.ProjectID
    inner join Fund f on pf.FundID = f.FundID where p.ProjectID = pr.ProjectID ) as  "Total Amount" from Project pr

     

    Can you guys please help me with the LINQ query for this.

    Thanks

  • Re: Sub Query /Getting SUM Using LINQ

    06-21-2009, 8:25 PM
    • Star
      12,555 point Star
    • malcolms
    • Member since 06-12-2008, 12:38 AM
    • Melbourne, Australia
    • Posts 2,061

    So you would need something like this: 

    using (DataContext dc = new DataContext())
    {
        var query = from p in dc.Project
        join pf in dc.ProjectFund on p.ProjectID equals pf.ProjectID
        join f in dc.Fund on p.ProjectID equals f.ProjectID 
                    select new
                    {
                        p.Name,
                        p.Description,
                        Sum = dc.Project.Sum(i => i.TotalAmount)
                    };
    }
    Give that a try. I can't test it because I don't have your tables.
     
    Sincerely,
    Malcolm Sheridan

    Microsoft Certified Solution Developer
    Please remember to click "Mark as Answer" on the post that helps you, and to click "Unmark as
    Answer" if a marked post does not actually answer your question.
  • Re: Sub Query /Getting SUM Using LINQ

    06-21-2009, 9:13 PM
    • Member
      12 point Member
    • learndontnet
    • Member since 03-05-2009, 3:51 AM
    • Posts 36

     Hey

     

    Thanks for your reply. 

    It helped me a lot better.. but the thing s its returning the sum value for all the records... its not summing up for all individual records (Projects). Basically, I need to get the Fund Amount for each project.. Our query you explained above is returning the sum of the fund amount for all projects.. so  its returning the same value for all records (Projects).

    If possible can you please help me out of this.

     

    BTW I have also modified your query like this:

     

    var query = from p in db.Projects
                            join pf in db.ProjectFunds on p.ProjectID equals pf.ProjectID
                            join f in db.Funds on pf.FundID equals f.FundID
                            where pf.ProjectID==p.ProjectID
                            orderby p.Name
                            select new
                            {
                                p.Name,
                                p.Description,
                                p.ProjectID,
                                p.DistrictID,
                                Sum = db.ProjectFunds.Sum(i => i.Fund.TotalAmount)
                            };

  • Re: Sub Query /Getting SUM Using LINQ

    06-21-2009, 9:31 PM
    Answer
    • Star
      12,555 point Star
    • malcolms
    • Member since 06-12-2008, 12:38 AM
    • Melbourne, Australia
    • Posts 2,061

    In that case you need to use a group by.  Something like this:

    var query = from p in db.Projects
                            join pf in db.ProjectFunds on p.ProjectID equals pf.ProjectID
                            join f in db.Funds on pf.FundID equals f.FundID
                            where pf.ProjectID==p.ProjectID
                            group p by new {dc.Projects.ProjectID} in g
                            orderby p.Name
                            select new
                            {
                                g.Key.ProjectID,
                                Count = g.Count,
                                Sum = db.ProjectFunds.Sum(i => i.Fund.TotalAmount)
                            }; 
     
    Sincerely,
    Malcolm Sheridan

    Microsoft Certified Solution Developer
    Please remember to click "Mark as Answer" on the post that helps you, and to click "Unmark as
    Answer" if a marked post does not actually answer your question.
  • Re: Sub Query /Getting SUM Using LINQ

    06-21-2009, 10:46 PM
    • Member
      12 point Member
    • learndontnet
    • Member since 03-05-2009, 3:51 AM
    • Posts 36

     Ok...Let me try with that ... I think I have basic Idea what you are saying.. I have one more question .. How can we get the DISTINCT records from the above query ..

  • Re: Sub Query /Getting SUM Using LINQ

    06-21-2009, 10:52 PM
    • Star
      12,555 point Star
    • malcolms
    • Member since 06-12-2008, 12:38 AM
    • Melbourne, Australia
    • Posts 2,061

    For a distinct try this:

    var query = (from p in db.Projects
    join pf in db.ProjectFunds on p.ProjectID equals pf.ProjectID
    join f in db.Funds on pf.FundID equals f.FundID
    where pf.ProjectID==p.ProjectID
    group p by new {dc.Projects.ProjectID} in g
    orderby p.Name
    select new
    {
    g.Key.ProjectID,
    Count = g.Count,
    Sum = db.ProjectFunds.Sum(i => i.Fund.TotalAmount)
    }).Distinct();
    Hopefully that works!  Let me know how you go.
    Sincerely,
    Malcolm Sheridan

    Microsoft Certified Solution Developer
    Please remember to click "Mark as Answer" on the post that helps you, and to click "Unmark as
    Answer" if a marked post does not actually answer your question.
  • Re: Sub Query /Getting SUM Using LINQ

    06-21-2009, 11:10 PM
    • Member
      12 point Member
    • learndontnet
    • Member since 03-05-2009, 3:51 AM
    • Posts 36

     Hey,

    Yeah.. that distinct works but now its not ordered with the "Name" .. Although I have included the statement OrderBy p.Name

  • Re: Sub Query /Getting SUM Using LINQ

    06-21-2009, 11:19 PM
    • Star
      12,555 point Star
    • malcolms
    • Member since 06-12-2008, 12:38 AM
    • Melbourne, Australia
    • Posts 2,061

    Okay try this:

    var query = (from p in db.Projects
                            join pf in db.ProjectFunds on p.ProjectID equals pf.ProjectID
                            join f in db.Funds on pf.FundID equals f.FundID
                            where pf.ProjectID==p.ProjectID
                            group p by new {dc.Projects.ProjectID, p.Name} in g                        
                            select new
                            {
                                g.Key.ProjectID,
                                g.Key.Name,
                                Count = g.Count,
                                Sum = db.ProjectFunds.Sum(i => i.Fund.TotalAmount)
                            }).Distinct().OrderBy(o => o.Name); 
    
    Sincerely,
    Malcolm Sheridan

    Microsoft Certified Solution Developer
    Please remember to click "Mark as Answer" on the post that helps you, and to click "Unmark as
    Answer" if a marked post does not actually answer your question.
  • Re: Sub Query /Getting SUM Using LINQ

    06-21-2009, 11:28 PM
    • Member
      12 point Member
    • learndontnet
    • Member since 03-05-2009, 3:51 AM
    • Posts 36

     Hey Finally I got the Sum for each record:

    var query = (from p in db.Projects
                            join pf in db.ProjectFunds on p.ProjectID equals pf.ProjectID
                            join f in db.Funds on pf.FundID equals f.FundID
                            //where pf.ProjectID==p.ProjectID                    
                            orderby p.Name descending                       
                            select new
                            {
                                p.Name,
                                p.Description,
                                p.ProjectID,
                                p.DistrictID,
                                Sum = db.ProjectFunds.Where(i => i.ProjectID==p.ProjectID).Sum(i => i.Fund.TotalAmount)
                            }).Distinct();

     

    Basically I have added another where in the sub query ,...

  • Re: Sub Query /Getting SUM Using LINQ

    06-21-2009, 11:32 PM
    Answer
    • Star
      12,555 point Star
    • malcolms
    • Member since 06-12-2008, 12:38 AM
    • Melbourne, Australia
    • Posts 2,061
    Cool.  I'm glad it's working.
    Sincerely,
    Malcolm Sheridan

    Microsoft Certified Solution Developer
    Please remember to click "Mark as Answer" on the post that helps you, and to click "Unmark as
    Answer" if a marked post does not actually answer your question.
  • Re: Sub Query /Getting SUM Using LINQ

    06-21-2009, 11:38 PM
    • Member
      12 point Member
    • learndontnet
    • Member since 03-05-2009, 3:51 AM
    • Posts 36

     Anyways thanks man you have been great help for me.. I have marked your reponses as answers twice ..

  • Re: Sub Query /Getting SUM Using LINQ

    06-21-2009, 11:41 PM
    • Star
      12,555 point Star
    • malcolms
    • Member since 06-12-2008, 12:38 AM
    • Melbourne, Australia
    • Posts 2,061
    I'm glad I could help.  If you have any other questions just send me an email.
    Sincerely,
    Malcolm Sheridan

    Microsoft Certified Solution Developer
    Please remember to click "Mark as Answer" on the post that helps you, and to click "Unmark as
    Answer" if a marked post does not actually answer your question.
Page 1 of 1 (12 items)