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 8840773
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T10:32:33+00:00 2026-06-14T10:32:33+00:00

I am using EntityFramework 4.3.1 and Database first with dbcontext. Let’s say I have

  • 0

I am using EntityFramework 4.3.1 and Database first with dbcontext.

Let’s say I have 2 tables:
Customer
and
Order
with a 1 to many relationship between Customer and Order.

I have two queries that I want to run using EntityFramework to get back specific information for different web pages.

The first is:
Get all Customers who have placed orders since a specific date.

So, the plain SQL for this query might be something like:

SELECT * FROM Customer WHERE CustomerID IN (
   SELECT CustomerID FROM Order WHERE OrderDate > '01-01-2012'
)

I can’t figure out how to do this in EntityFramework.

I started with the following code, but can’t figure out where to go from here.

var customersWithOrdersAfterGivenDate = this._dbContext.Customer
   .Include("Order")
   .Where(c => ??? What do I do here ??? );

I tried to do (c.Order.OrderDate > someDate), but this doesn’t work because I can’t get the OrderDate property from Order since EntityFramework is treating Order as a collection.

The second problem is:
Get the total number of orders for a given collection or Customers

The plain SQL for this query might look like:

SELECT CustomerID, COUNT(*)
FROM Order
WHERE CustomerID IN (*someCollectionOfIDs*)
GROUP BY CustomerID

Again, I have no idea how to do this in EntityFramework. How do I get only the CustomerID and the total count of records for the CustomerID from the Order table? I can’t figure out how to do a group by so that it doesn’t just return every single order record for each customer.

———————————-


Update:
Here’s my final code after feedback from Eli.

To get Customers with Orders that were created after a given date:

var customerIDs = dbContext.Customer
   .Include("Order")
   .Where(c => c.Order.Any(o => o.DateCreated.CompareTo(startDate) > 0))
   .Select(s => new
      {
         c.CustomerID
      })
   .AsEnumerable()
   .Select(cust => cust.CustomerID)
   .ToList<int>();

To get total count of Orders for each Customer in a given collection of Customers:

List<int> customerIDs = GetCustomerIDsToQuery();

var orderTotals = dbContext.Orders
   .Where(o => customerIDs.Contains(o.CustomerID))
   .GroupBy(o => o.CustomerID)
   .Select(o => new
      {
         CustomerID = o.Key,
         TotalOrders = o.Count()
      })
   .AsEnumerable();

From the above, I can iterate through the collection and get the values from the anonymous type:

foreach(var ot in orderTotals)
{
   var customerID = ot.CustomerID;
   var totalOrders = ot.TotalOrders;
   //Do something with these values...
}
  • 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-14T10:32:35+00:00Added an answer on June 14, 2026 at 10:32 am

    Your SQL also contains a nested collection, so your LINQ will be the same. Something along the lines of .Where(c => c.Orders.Any(order => order.OrderDate > someDate));

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm new to Entity Framework and am using Database First. I have inherited a
I'm using Entity Framework 4.2 (EntityFramework.dll v4.0.30319) Code First and have a LINQ query
I am using Entity Framework 4.3.1 code first. I have 2 tables in my
I am using entity framework with database first approach, along with EF 4.x DBContext
I'm using Entity Framework (DbContext with database first) with MVC. When user save from
I'm using Entity Framework 4.3 Code First with a custom database initializer like this:
I am creating a new database model using Entity Framework 4.3 Code-FIrst; using Fluent
I have created a new application using Entity Framework 4.3 database migrations. The migrations
I am using EntityFramework 4.1. I have the following model: public class User {
I'm using EntityFramework code first with migrations. From the package manager console, I'm running

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.