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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T05:36:14+00:00 2026-05-28T05:36:14+00:00

I have 2 tables, one Orders and one Products. The Orders has OrderID, OrderName

  • 0

I have 2 tables, one Orders and one Products. The Orders has OrderID, OrderName and AccessID, whilst the Products has ProductID, ProductName and OrderID. These tables have more fields but these are the relevant ones for my problem.

So what I wish to do is, get the first set of products in the products table with their OrderID, which order has less than 8 products, and the AccessID is 1.

I know that I can do it in 2 LINQ statements, first filter by AccessID in Orders, and then create a loop in the products table to get the first instance of Count < 8, however I am sure there is a better way to do this, possibly in 1 statement.

Thanks for your help and time

This is what I have come up with for now and still has to be tested :-

    public Order OrdersLessThanEightItems()
    {
        IEnumerable<int> orderId = null;
        int accessID = 1;

        Order order = (from o in db.Orders
                                     where o.AccessID == accessID && o.Products.Count < 8                                         
                                     orderby o.Products.Count ascending 
                                     select o).FirstOrDefault();
        return order;
    }
  • 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-28T05:36:15+00:00Added an answer on May 28, 2026 at 5:36 am

    Pre Edit

    Shouldn’t the relationship between orders and products be a many to many relationship?

    Should product have OrderID? Then 1 product will only allowed to be in 1 order.

    Post Edit (after highlighting that the relationship is 1 to many)

    public class Product 
    {
       //...
       public Order InOrder { get; set; }
       //...
    }
    
    public class Order
    {
       //...
       public virtual ICollection<Product> ProductsInOrder { get; set; }
       //...
    }
    

    Then you’ll be able to use this (untested):

    List<Product> productList = (from o in context.Orders
                                 where o.AccessID == myAccessID && o.ProductsInOrder.Count < 8
                                 orderby o.ProductsInOrder.Count
                                 select o).ToList();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two tables Orders and Products where Orders has a ProductID as a
I have two tables: products and orders. Orders references products via ProductID as a
I've often seen line_item tables for orders or invoices that copy one or more
I have a table of orders. Each order is linked to one or more
I have one table orders with a foreing key ProductID. I want to show
Lets say I have a Customers table and an Orders table with a one-to-many
Lets say I have data in two tables. In one I have Order ID
I have two tables one with ID and NAME table 1 ID | NAME
I have two tables one called fs_note the other called dumy_fs_note I created after
i have two tables one is called addons and holds information about different addons,

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.