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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T22:04:40+00:00 2026-05-18T22:04:40+00:00

Using Entity Framework 4 CTP5 I have a basic model and a basic DbContext

  • 0

Using Entity Framework 4 CTP5 I have a basic model and a basic DbContext that works

public class Customer {
  public int CustomerId { get; set; }
  public int Name { get; set; }
  //...
  public ICollection<Address> Addresses { get; set; }
  public bool HasAddress {
    get {
      return Addresses.Count > 0;
    }
  }
}

public class Address {
  public int AddressId { get; set; }
  public string StreetLine1 { get; set; }
  //....
  public Customer Customer { get; set; }
}

How can I query my DbContext to return all customers and whether they have an address?

A customer can have multiple addresses and I don’t want to return all the addresses for each customer when I am only interested in whether they have an address or not. I use
context.Customers.Include(c => c.Addresses) but that returns all addresses for each customer

  • 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-05-18T22:04:40+00:00Added an answer on May 18, 2026 at 10:04 pm

    Keep in mind, in order to determine if a Customer has addresses, an aggregate operation (COUNT) must be done on the server. I don’t think you can use that HasAddress property directly in the LINQ predicate, as AFAIK aggregate operations cannot be used in LINQ-Entities predicates.

    So you might have to do something like this:

    var result = ctx.Customers.Select(x => new
                                      {
                                         Customer = x,
                                         HasAddress = x.Addresses.Count() > 0
                                      }).ToList();
    

    That will return a collection of anonymous types, including all Customers, and whether they have at least one address.

    Note the () on the Count – this performs an actual aggregate operation – whilst .Count is a client-side LINQ operation.

    Edit

    If you want to put that anonymous type back into a Customer object, you can do this after the query has materialized (e.g ensuring the .Count() has been done on the database):

    var result = ctx.Customers.Select(x => new
                                          {
                                             Customer = x,
                                             HasAddress = x.Addresses.Count() > 0
                                          }).ToList()
                                          .Select(x => new Customer
                                          {
                                             // left to right copy....
                                             HasAddress = true
                                          }).ToList();
    

    Which is pretty dirty IMO, but it’ll work.

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

Sidebar

Related Questions

Using Entity Framework with MVC2, I have a series of date textboxes, that I
I'm using entity framework 4. I have a stored procedure that just updates one
I'm using Entity Framework and I have created an interface for lease transactions: public
I am using Entity Framework with my website. To improve performance, I have started
I have a problem with the following Linq query using Entity Framework: from o
So, I am using the Linq entity framework. I have 2 entities: Content and
Using Entity Framework I have the fields: o CreatedOn (datetime) o CreatedBy (nvarchar(50)) o
I am using entity framework with a web service and I have entity partial
I'm having trouble mapping two entities together with Entity Framework CTP5 using Code First
Using Entity Framework Code First CTP5, how do I create a primary key column

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.