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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T21:41:34+00:00 2026-05-27T21:41:34+00:00

I am looking for a nice simple LINQ query that can get an item

  • 0

I am looking for a nice simple LINQ query that can get an item that is in a collection of a collection. Consider the following models…

class Customer
{
   List<Order> Orders;
   int ID;
}

class Order
{
   int ID;
}

Lets say I have a list of type customer…

List<Customer> Customers;

What I want is a single Order based on the ID of the Order (this is my known input data). I also can assume that an Order is unique to a customer (so there will only be one Order with the ID I am looking for across all customers)

Is there a nice LINQ query I can use to get the Order I am looking for?

I know I can easily do this over a single list of Orders, but this is the set up I have to work with. I do not want to be querying for more data, ff I already have enough in memory to get what I need.

I could do this with a semi-messy for loop. Maybe I should just do this? Maybe I am trying to simplify the code too much?

  • 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-27T21:41:34+00:00Added an answer on May 27, 2026 at 9:41 pm

    Oops, misread the question:

    var matchingOrders = from customer in customers
                         from order in customer.Orders
                         where order.ID == orderID
                         select order;
    
    var order = matchingOrders.Single();
    

    As in an earlier version of this answer, note that this will fail if there are no such matches or more than one. You may also want to look at SingleOrDefault, First, and FirstOrDefault.

    This code can also be written without query expressions using SelectMany:

    var order = customers.SelectMany(customer => customer.Orders)
                         .Single(order => order.ID == orderID);
    

    Note that you don’t need to use Where and then Single – there’s a Single overload taking a predicate (and likewise for FirstOrDefault etc). Whether you use the query expression form or not is up to you – I usually use query expressions rather than SelectMany, but in this case you don’t need the customer, so you can use the simple overload of SelectMany.

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

Sidebar

Related Questions

What is BPEL? I'm looking for some nice simple examples of BPEL usage. The
I am looking for nice looking sites that are good in terms of look
I found several controlsets for that nice looking ribbons (DotNetBar, DivElements, ...), but all
I don't find the oft-used * to be very nice looking - can anyone
I am developing a multilingual web application that has a nice looking UI. I
I'm looking for a simple process-based parallel map for python, that is, a function
I am looking for a simple tool that allows the translators to edit .resx
I'm looking for something nice and simple. For example: RunAfter(3, delegate() { z =
I am looking for an simple online text editor that will load an online
Can anyone recommend a nice simple, easy to use source control system I can

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.