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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T06:40:23+00:00 2026-05-11T06:40:23+00:00

I want to write an elegant linq query to handle the following SAMPLE object

  • 0

I want to write an elegant linq query to handle the following SAMPLE object model:

    class Category     {         public string Name { get; set; }                 public IList<Product> Products { get; set;}             }      class Product     {         public string Title { get; set; }         public IList<Photo> Photos { get; set; }     }      class Photo     {         public int Id { get; set; }     } 

I constructed the following query to get the Photo Id:

    var query = from category in Factory.GetCategories()                 where category.Name == 'Cameras'                 select (from product in category.Products                         where product.Title == 'Sony'                         select (from photo in product.Photos                                 select photo.Id)                                );      var v = query.ToList(); 

At the moment the query does not project correctly i have to add a FirstOrDefault() to each of the Sub Selects!:

var query = from category in Factory.GetCategories()                 where category.Name == 'Cameras'                 select (from product in category.Products                         where product.Title == 'Sony'                         select (from photo in product.Photos                                 select photo.Id).FirstOrDefault()                                ).FirstOrDefault();  var v = query.ToList(); 

Is there a better way to do this? Ignoring the fact that we are not dealing with a database and PK’s/FK’s are not in play.

I really want to avoid writing a big for loop when i could do the same thing in a linq query:

foreach (var category in Factory.GetCategories())             {                 if (category.Name == 'Camera')                 {                     foreach (var product in category.Products)                     {                         if (product.Title == 'Sony')                         {                             foreach (var photo in product.Photos)                             {                                 //get data                                 int id = photo.Id;                             }                         }                     }                 }                             } 

The actual implementation is more complex than this simple object model. I want to get the basic idea from this simple sample so i apply it to my real object model.

Cheers!

  • 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. 2026-05-11T06:40:24+00:00Added an answer on May 11, 2026 at 6:40 am

    Do you just want the flattened ids?

            var query = from category in Factory.GetCategories()                     where category.Name == 'Cameras'                     from product in category.Products                     where product.Title == 'Sony'                     from photo in product.Photos                     select photo.Id; 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm following an example in Accelerated C++ and writing a simple Handle class that
I want to write a Swing application in Griffon but I am not sure
I want to write a command that specifies the word under the cursor in
I want to write a little DBQuery function in perl so I can have
I want to write a function in Python that returns different fixed values based
I want to write a function that takes an array of letters as an
I want to write a word addin that does some computations and updates some
I want to write some JavaScript that will change the onmousedown of a div
I want to write a real-time analysis tool for wireless traffic. Does anyone know
I want to write a raw byte/byte stream to a position in a file.

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.