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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T02:04:15+00:00 2026-05-14T02:04:15+00:00

My knowledge of Lambda expressions is a bit shaky, while I can write code

  • 0

My knowledge of Lambda expressions is a bit shaky, while I can write code that uses Lambda expressions (aka LINQ), I’m trying to write my own method that takes a few arguments that are of type Lambda Expression.

Background: I’m trying to write a method that returns a Tree Collection of objects of type TreeItem from literally ANY other object type. I have the following so far:

public class TreeItem
{
    public string Id { get; set; }
    public string Text { get; set; }

    public TreeItem Parent { get; protected set; }

    public IList<TreeItem> Children
    {
        get
        {
            // Implementation that returns custom TreeItemCollection type
        }
    }

    public static IList<TreeItem> GetTreeFromObject<T>(IList<T> items,
        Expression<Func<T, string>> id,
        Expression<Func<T, string>> text,
        Expression<Func<T, IList<T>>> childProperty) where T : class
    {
        foreach (T item in items)
        {
           // Errrm!?? What do I do now?
        }

        return null;
    }
}

…which can be called via…

IList<TreeItem> treeItems = TreeItem.GetTreeFromObject<Category>(
    categories, c => c.Id, c => c.Name, c => c.ChildCategories);

I could replace the Expressions with string values, and just use reflection, but I’m trying to avoid this as I want to make it strongly typed.

My reasons for doing this is that I have a control that accepts a List of type TreeItem, whereas I have dozens of different types that are all in a tree like structure, and don’t want to write seperate conversion methods for each type (trying to adhere to the DRY principle).

Am I going about this the right way? Is there a better way of doing this perhaps?

  • 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-14T02:04:16+00:00Added an answer on May 14, 2026 at 2:04 am

    There’s no such type as “lambda expression”. A lambda expression can either be converted into a compatible delegate type, or an expression tree.

    Your existing method signature uses expression trees – but it’s not at all clear that it really needs to. Try the delegate form (with a few parameter name changes):

    public static IList<TreeItem> GetTreeFromObject<T>(IList<T> items,
        Func<T, string> idSelector,
        Func<T, string> textSelector,
        Func<T, IList<T>> childPropertySelector) where T : class
    

    Then you can do something like this:

    foreach (T item in items)
    {
        string id = idSelector(item);
        string text = textSelector(item);
        IList<T> children = childPropertySelector(item);
        // Do whatever you need here
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

A little knowledge can be a dangerous thing. Now that I've had a run
My knowledge of the intel instruction set is a bit rusty. Can you tell
It is common knowledge that the exec method from RegExps can get used to
My knowledge on common table expressions is lacking at the moment so any help
My XQuery knowledge is pretty lacking but I'm trying to play around with XQIB
My knowledge of code just doesn't go this deep, and I have no idea
I'd need to be able to combine 2 lambda expressions into 1: This would
What I am trying to do seems simple, but I can't find a way.
My knowledge of C++ arcana is a bit rough. Let's say I have the
It is almost common knowledge that one should always compile with -Wall . What

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.