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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T02:52:06+00:00 2026-05-18T02:52:06+00:00

I having a hierarchy collection object where I am trying to retrieve the last

  • 0

I having a hierarchy collection object where I am trying to retrieve the last level object’s property in Linq. I dont want to write a get method for every property. Not sure how to achieve it through selector

Class Test {
    public int ID { get; set; }
    public int PopertyA { get; set; }
    public string PopertyB { get; set; }
              ...Many more properties

}

public static TResult GetTest(hierarchyObject, int ID, Func<TSource, TResult> selector)
{
    return (from level1 in hierarchyObject.Level1
            from test in level1.Test
            where test.ID.Equals(ID)
            select selector).First();
}

This dint work. Currently I have made the method to return the test object and accessing the properties in the calling method. But wanted to know if I can implement a generic property getter.

Edit:

Class Hierarcy{
  public IList<Level1> level1;
}

Class Level1 {
public IList<Test> test;
}

Given a hierachy object and test.ID, I want to retrieve any property of Test.

  • 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-18T02:52:06+00:00Added an answer on May 18, 2026 at 2:52 am

    It depends on what you want to do with your property. To avoid repeating the entire LINQ query for each and every property you are interested in, it would be better to get the Test object first, and then check its individual properties:

    class Hierarcy
    {
       public IList<Level1> Level1;
       public Test GetTest(int ID)
       {
           return this
              .Level1
              .SelectMany(level => level.Test)
              .Where(test => test.ID == ID)
              .First();
       }
    }
    

    Once you get the Test class, you have all its properties:

    Test t = myHierarchy.GetTest(someId);
    
    // do something
    int i = test.PropertyA;
    string s = text.PropertyB;
    

    If you are interested in getting a value of a property dynamically, using its name only, you can do it using Reflection:

    Test t = myHierarchy.GetTest(someId);
    
    // this will print all properties and their values
    foreach (PropertyInfo pi in t.GetType().GetProperties())
    {
        Console.WriteLine("Name:{0}, Value:{1}",
           pi.Name,
           pi.GetValue(pi, null));
    }
    

    In both examples, actual query is executed only once, which can be significant if there are lots of objects in your collections.

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

Sidebar

Related Questions

I have a pretty deep object hierarchy in my application, and I am having
I am having some issues with some Linq again where i am trying to
How to load & get collection of all active-record models used inside rails app.
I am having extreme difficulty constructing a query which returns an XML style hierarchy.
I have the following class hierarchy: class A(object): def __init__(self, filename, x): self.x =
Today I faced an interesting issue. I've been having an inheritance hierarchy with Hibernate
I have a .net solution having a reference hierarchy like this MyWinApp->ServerCore->DataAccess where the
I'm having a problem with C++ inheritance. I have a class hierarchy: class A
I am trying to flatten a hierarchy / structure to XML using XSLT 1
I have an 3 object hierarchy which has a root as 'Division' that has

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.