Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8395669
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T20:21:30+00:00 2026-06-09T20:21:30+00:00

I am using LINQ to SQL in a project and I am having an

  • 0

I am using LINQ to SQL in a project and I am having an issue doing both a join and a group by to do a comparison between the two fields that are in each table.

Here is what my query looks like:

var q =
   (from ii in
        (from a in table1
         join b in table2 on a.BudgetUnitID equals b.BudgetUnitID
         select new { BT = a.Amount, BA = b.Amount, BUID = a.BudgetUnitID, BU = a.BudgetUnit.BudgetUnitName })
    group ii by new {ii.BUID} into g
    select new
    {
        BudgetUnit = g.Key,
        Budget = g.Sum(x => x.BA),
        Actual = g.Sum(x => x.BT),
        Variance = g.Sum(x => x.BA) - g.Sum(x => x.BT)
    }).ToList();

I am am going to bind this to a grid view on a web page. My problem is that I am not getting the totals correct on one of the columns.

Your help is much appreciated.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T20:21:31+00:00Added an answer on June 9, 2026 at 8:21 pm

    Your problem is likely that the Budget amount is incorrect, because you are summing them for every combination. For example, if you had budget data looking like this:

    Budget | Amount
    Dev    | 2500
    

    And line items like this:

    Budget | Amount
    Dev    | 1000
    Dev    | 750
    

    Then you are combining them together in your inner query like this:

    BA   | BT
    2500 | 1000
    2500 | 750
    

    And then summing them together, which gives you

    Budget | Actual
    5000   | 1750
    

    In this case, the budget is twice as large as it should be, because you are adding all the entries together per combination. You want to sum the individual tables first, before joining the results together so that you don’t have this “duplication” issue in your sub-query data:

    var q =
       (from ii in
            (from a in table1 group a by a.BudgetUnitID into g
             select new { BudgetUnitID = g.Key, Amount = g.Sum(x => x.Amount) })
        join jj in
            (from b in table2 group b by b.BudgetUnitID into g
             select new { BudgetUnitID = g.Key, Amount = g.Sum(x => x.Amount) })
            on ii.BudgetUnitID equals jj.BudgetUnitID
             select new { Actual = ii.Amount, Budget = jj.Amount, Variance = jj.Amount - ii.Amount, BUID = ii.BudgetUnitID }
        ).ToList();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a short question. Im my current project I'm using LINQ-to-SQl. That is
I am using LINQ to SQL in my project. I have two tables Category
I am using Linq to SQL in a two tier project where the server
I'm running into an issue on a brownfield project that is using LINQ to
I have started using Linq to SQL for a project im working on and
I'm using LINQ to SQL to speed up delivery of a project, which it's
We are using Linq-to-SQL with SQL Server as an ORM on our new project.
I am creating serialized XML for a LINQ to SQL project using the DataContractSerializer
How to select all columns from tables in join using linq Sql: select CTRL_RUN_JOB.*,
I'm trying to use Linq to SQL to return an IQueryable(of Project) when using

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.