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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T01:44:13+00:00 2026-05-26T01:44:13+00:00

I have a simple data model and I am using EF4.1 code first (but

  • 0

I have a simple data model and I am using EF4.1 code first (but the problem seems to be relevant to any EF4 approach) as an ORM to store and retrieve data.
The model looks somewhat like:

public class Customer
{
    public int CustomerId { get; set; }
    public string Name { get; set; }
    public virtual List<Product> Products { get; set; }
}

public class Product
{
    public int ProductId { get; set; }
    public string Name { get; set; }
    public virtual ICollection<Event> Events { get; set; }
    public virtual ICollection<Customer> Customers { get; set; }
}

public class Event
{
    public int EventId { get; set; }
    public string Name { get; set; }
    public virtual List<Product> Products { get; set; }
}

So the entity relationships are like:

Customer —– Product —– Event

An event affects N Products (eg. a raise in price or a deal on a product)
Each Customer has N Products belonging to him.

Now for every Event in the database I would like to get a set (note non duplicate entries) of Customers affected by this Event. A customer is affected by the event if he has at least one product affected by the event.

The naive implementation would be:

foreach (var ev in context.Events)
{
  foreach(var product in ev.Products)
  {
    //Check for customers with this product and add them to the list of affected customers
  }
}

But this seems like an anti-pattern. The question is:
How to write a query to solve this problem in a more effective manner? (maybe linq, but even native SQL would help)?

  • 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-26T01:44:14+00:00Added an answer on May 26, 2026 at 1:44 am

    You might be looking for a query like this:

    var query = from ev in context.Events
                select new
                {
                  Event = ev,
                  AffectedCustomers = (from pr in ev.Products
                                       from cu in pr.Customers
                                       select cu).Distinct()
                };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using core data and I have a simple two-entity model. My navigation controller
I have a simple data model of two tables, email and recipients, email can
I have a simple data model in JPA (hibernate) consisting of many to one
I am using the new ADO.NET Entity Data Model on a simple database. I
I have hierarchical data stored in the datastore using a model which looks like
I have only simple data types in method signature of service (such as int,
I have a simple WCF Data Services service and I want to expose a
I have simple SSIS package which reads data from flat file and insert into
I have simple SSIS package where I import data from flat file into SQL
If I have a simple piece of data to store (an integer or string

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.