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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T06:33:24+00:00 2026-05-25T06:33:24+00:00

I am having some issues with some Linq again where i am trying to

  • 0

I am having some issues with some Linq again where i am trying to restrict the information coming back but still have the objects in a form of hierarchy (based on the xml its comming from and what is needs to do in the UI) the basic format is this:

  1. Single Room
    • [Rooms Collection]
      • Room 1 (ID = 1)
      • Room 2 (ID = 2)
  2. Twin Room
    • [Rooms Collection]
      • Room 3 (ID = 3)
      • Room 4 (ID = 4)

but what i want is based on the room id (1,2,3,4 etc) return the room type and the specific room hierachy i.e (if id 4 was passed in).

  1. Twin Room
    • Room 4 (ID = 4)

i created some LINQ which i thought was working, but i was wrong as it simply returns the first Room Type (Single Room) and only a room if the if matches (which i send id 3 and 4 it doesnt) and looking at it i fully understand that.

In the code below items is coming from another method which basically outputs in the example hierachy above:

            var item = items.Select(i =>                
            new RoomType
            {
                name = i.name,
                rooms = i.rooms
                    .Where(r => r.Name == id.ToString())
            }).FirstOrDefault();

What i need to do is return only the room type that has a room in its room collection which has the correct id that i am passing into my method, i am not sure if the code above is a start, i tried messing around with Contains etc but i am searching for an id when Contains is expecting an object so i am not sure what the best approach is here, and i am stuck.

Any help would be great.

Rob

  • 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-25T06:33:24+00:00Added an answer on May 25, 2026 at 6:33 am
    var item = items.Where(i => i.rooms.Any(r => r.Name == id.ToString())
                    .Select(i =>                
                               new RoomType
                               {
                                   name = i.name,
                                   rooms = i.rooms
                                            .Where(r => r.Name == id.ToString())
    
                               }).SingleOrDefault();
    

    You should filter out room types that don’t contain your room with the particular id (that’s what the Where with the Any call in it does).

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

Sidebar

Related Questions

I have been having some issues with LINQ-To-SQL around memory usage. I'm using it
having some issues with a callback request form I'm trying to build for a
having some doubts with LINQ queries over objects... I have this filter descriptions which
Ok I am still having some issues with this code below. I have received
I am having some issues with using the OrderBy extension method on a LINQ
I am having some issues trying to convert a double to C++ string. Here
Having some issues getting this MPMoviePlayerViewController to work. I have two sample URLs pointing
I am trying to get facebook wall but I am having issues with JSON
I'm having some issues with a bit of LINQ syntax and I believe I'm
Having some issues with this search engine I am trying to put together. I'll

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.