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 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 have a linq query involving the following 3 entities: public class LandPoint {
I want to write the following query: SELECT ..., MIN(SomeBitField), ... FROM ... WHERE
I want write a simple query which will fetch data from a table (which
I want to write a procedure to perform a delete for the following i
I want to write a class with three int values in them and manipulate
I want to write a method, which takes a String and swaps each pair
I want write a little code analyzer which parses nested structures and translates into
Hello I want write my own desktop sharing application in Java. The application should
I want to write a batch script statement where: FINDSTR has to check for
I want to write a program in Prolog that confirms if a b-tree of

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.