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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T23:58:51+00:00 2026-05-13T23:58:51+00:00

I have the following linq query (applied to the Northwind database) (from od in

  • 0

I have the following linq query (applied to the Northwind database)

(from od in OrderDetails
where od.ProductID == 11 || od.ProductID == 42
select od.OrderID).Distinct()

Which gives me a list of Order Ids (67 items) where the order includes either product 11 or 42. How can I rewrite the query to give me a list of Order Ids where the order includes both product 11 and 42? The resulting list should only feature one order (orderid = 10248)

Obviously the following query does not return any orders.

(from od in OrderDetails
    where od.ProductID == 11 && od.ProductID == 42
    select od.OrderID).Distinct()

Here is a sql query that does the job but what is the best (or most efficient) way of writing it in linq?

    SELECT DISTINCT OrderID
    FROM         [Order Details]
    WHERE     (OrderID IN
                              (SELECT     OrderID
                                FROM          [Order Details] AS OD1
                                WHERE      (ProductID = 11))) AND (OrderID IN
                              (SELECT     OrderID
                                FROM          [Order Details] AS OD2
                                WHERE      (ProductID = 42)))

[edit]

Thanks to klausbyskov for his solution. From that i was able to build an expression (using PredicateBuilder) that can take a dynamic list of product ids, use them in the where clause and return a list of orders. Here it is if anyone is interested.

public static Expression<Func<Order, bool>> WhereProductIdListEqualsAnd( int[] productIds )
{
    var condition = PredicateBuilder.True<Order>();

    foreach ( var id in productIds )
    {
        condition = condition.And( o => o.OrderDetails.Any( od => od.ProductId == id ) );
    }

    return condition;
}
  • 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-13T23:58:51+00:00Added an answer on May 13, 2026 at 11:58 pm

    Start the query on the order relation instead:

    var result = Orders.Where(o => o.OrderDetails.Any(od => od.ProductId == 11) 
                                && o.OrderDetails.Any(od => od.ProductId == 42));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following LINQ query: var queryGroups = (from p in db.cl_contact_event select
I have the following LINQ query that returns two objects from my database. These
I have the following LINQ query: From metasect In DirectCast(Form.ChildDocBox.Tag, META_DOCUMENT).META_SECTIONS From metaset In
I have this following IEnumerable LINQ query: var query = from p in Enumerable.Range(2,
I have the following XML LINQ query from my XDocument. var totals = (from
I have the following Nhibernate LINQ query: var query = from c in session.Query<Customer>()
I have the following LINQ query: vm.Ter = (from tr in DataContext.Terr_Rp join dm
I have the following LINQ query: Manager mngr = (from tr in DataContext.Manager where
I have the following LINQ query - var data = (from req in db.tblRequirements
I have the following LINQ query which uses NHibernate against a SQL Server backed

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.