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

  • Home
  • SEARCH
  • 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 8275607
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T08:07:48+00:00 2026-06-08T08:07:48+00:00

I have 3 tables, Cust, Order and Item with the following relevant fields: Cust

  • 0

I have 3 tables, Cust, Order and Item with the following relevant fields:

Cust - CustID, CustName
Order - OrderID, CustID
Item - ItemID, OrderID

I want to find the total number of orders and items for each customer.

Here is an SQL statement that generates what I want, a list of customers with the total number of orders for each customer and the total number of items ordered.

SELECT
  Cust.CustID, Cust.CustName,
  count(DISTINCT Order.OrderID) AS numOrders,
  count(DISTINCT Item.ItemID ) AS numItems
FROM Cust
LEFT JOIN Order ON Order.CustID = Cust.CustID
LEFT JOIN Item ON Item.OrderID = Order.OrderID
GROUP BY Cust.CustID, Cust.CustName
ORDER BY numItems

My first attempt at converting this to LINQ was to just count items and came up with this:

var qry = from Cust in tblCust
    join Order in tblOrder on Cust.CustID equals Order.CustID
    join Item in tblItem on Order.OrderID equals Item.OrderID
    group Cust by new {CustID = Cust.CustID, CustName = Cust.CustName} into grp
    orderby grp.Count()
    select new
    {
        ID = grp.Key.CustID,
        Name = grp.Key.CustName,
        Cnt = grp.Count()
    };

With this code I get the exception:

Value cannot be null. Parameter name: inner

Am I on the right track? What would I have to do to get both counts?

  • 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-08T08:07:49+00:00Added an answer on June 8, 2026 at 8:07 am
    1. For Left Joins – I suggest using a from with a where and a DefaultIfEmpty

    2. You need to group using an anonymous type in order to group multiple parameters

    Value cannot be null. Parameter name: inner

    Are any of joining properties nullable?

     var qry = 
            from Cust in tblCust
            from Order in tblOrder.Where(x => Cust.CustID == x.CustID)
                                  .DefaultIfEmpty()
            from Item in tblItem.Where(x => Order.OrderID == x.OrderID)
                                .DefaultIfEmpty()
            group new { Cust, Order.OrderId, Item.ItemId } by new { Cust.CustID, Cust.CustName } into grp
            let numItems = grp.Select(x => x.ItemId).Distinct().Count()
            orderby numItems 
            select new
            {
                ID = grp.Key.CustID,
                Name = grp.Key.CustName,
                numOrders = grp.Select(x => x.OrderId).Distinct().Count(),
                numItems,
            };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have three tables: CustOrder : id, CreateDate, Status DenominationOrder : id, DenID, OrderID
I have the following example in a SQL table Cust Group Sales A 1
I have an typical CUSTOMER/ORDERS set of tables and I want to display the
I have this following problem: I have two tables 1 table for customer bought
I have the following code that will join two different tables together and provide
I have the following tables customers cust_id cust_name 1 a company 2 a company
I have a SQL database with has the following: Customer, Item, Clothing and Food.
I have the following tables: Category table which has an ID column, a description
I have tables A and B with identity PKs ida and idb : ex
I have tables like these two test_table date student test 2012-05-31 Alice Math 2012-05-31

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.