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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T03:47:29+00:00 2026-05-20T03:47:29+00:00

From what I understand from the documentation of SelectMany, one could use it to

  • 0

From what I understand from the documentation of SelectMany, one could use it to produce a (flattened) sequence of a 1-many relationship.

I have following classes

  public class Customer
  {
    public int Id { get; set; }
    public string Name { get; set; }
  }

  class Order
  {
    public int Id { get; set; }
    public int CustomerId { get; set; }
    public string Description { get; set; }
  }

I then try to use them using the query expression syntax like so

  var customers = new Customer[]
  {
    new Customer() { Id=1, Name ="A"},
    new Customer() { Id=2, Name ="B"},
    new Customer() { Id=3, Name ="C"}
  };

  var orders = new Order[]
  {
    new Order { Id=1, CustomerId=1, Description="Order 1"},
    new Order { Id=2, CustomerId=1, Description="Order 2"},
    new Order { Id=3, CustomerId=1, Description="Order 3"},
    new Order { Id=4, CustomerId=1, Description="Order 4"},
    new Order { Id=5, CustomerId=2, Description="Order 5"},
    new Order { Id=6, CustomerId=2, Description="Order 6"},
    new Order { Id=7, CustomerId=3, Description="Order 7"},
    new Order { Id=8, CustomerId=3, Description="Order 8"},
    new Order { Id=9, CustomerId=3, Description="Order 9"}
  };

  var customerOrders = from c in customers
                       from o in orders
                       where o.CustomerId == c.Id
                       select new 
                              { 
                                 CustomerId = c.Id
                                 , OrderDescription = o.Description 
                              };

  foreach (var item in customerOrders)
    Console.WriteLine(item.CustomerId + ": " + item.OrderDescription);

This gives to what I need.

1: Order 1
1: Order 2
1: Order 3
1: Order 4
2: Order 5
2: Order 6
3: Order 7
3: Order 8
3: Order 9

I assume this translates to using the SelectMany method when not using the query expression syntax?

Either ways, I’m trying to wrap my head around using SelectMany. So even if my above query does not translate to SelectMany, given the two classes and mock data, could someone provide me with a linq query that uses SelectMany?

  • 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-20T03:47:30+00:00Added an answer on May 20, 2026 at 3:47 am

    Here is your query using SelectMany, modeled exactly after your example. Same output!

            var customerOrders2 = customers.SelectMany(
                c => orders.Where(o => o.CustomerId == c.Id),
                (c, o) => new { CustomerId = c.Id, OrderDescription = o.Description });
    

    The first argument maps each customer to a collection of orders (completely analagous to the ‘where’ clause you already have).

    The second argument transforms each matched pair {(c1, o1), (c1, o2) .. (c3, o9)} into a new type, which I’ve made the same as your example.

    So:

    • arg1 maps each element in the base collection to another collection.
    • arg2 (optional) transforms each pair into a new type

    The resulting collection is flat like you’d expect in your original example.

    If you were to omit the second argument, you would end up with a collection of all orders the match up to a customer. It’d be just that, a flat collection of Order objects.

    Using it takes a lot of getting used to, I still have trouble wrapping my head around it sometimes. 🙁

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

Sidebar

Related Questions

I understand from the official documentation on unowned relationships that the app must use
I have a hard time to understand STArray from the documentation and other howtos/discussion
Using a delegate I can call any function asynchronously. From the documentation I understand
I understand what wrappers are, but from the documentation you seem to be able
From what I understand, in TDD you have to write a failing test first,
From what I understand in the documentation, it returns null if it fails, but
I'm trying to understand the documentation from Android for Sensor.TYPE_ACCELEROMETER: It gives this example:
I want to use SHA1 function from Linux libraries ( documentation ) which returns
Just a quick question, as I couldn't quite understand it from the documentation. If
As far as I can understand from the java.lang.Thread docs, and from other questions

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.