Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Process Lambda Expressions Passed as Argument Into Method - C# .NET 3.5

Tags:

c#

.net

lambda

linq

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?

like image 631
Sunday Ironfoot Avatar asked Mar 29 '10 11:03

Sunday Ironfoot


People also ask

How do you convert lambda expression to method?

To convert a lambda expression to a named methodFrom the Refactor menu of the VisualAid choose To Named Method. Telerik® JustCode™ will replace the lambda expression with a method group and will add a new method.

How do you pass a lambda expression as a parameter?

Passing Lambda Expressions as Arguments If you pass an integer as an argument to a function, you must have an int or Integer parameter. If you are passing an instance of a class as a parameter, you must specify the class name or the object class as a parameter to hold the object.

Which method you can pass lambda expression?

If we need to pass a lambda expression as an argument, the type of parameter receiving the lambda expression argument must be of a functional interface type. In the below example, the lambda expression can be passed in a method which argument's type is "TestInterface".

Does lambda expression enables functions to be passed as argument?

lambda expressions are added in Java 8 and provide below functionalities. Enable to treat functionality as a method argument, or code as data. A function that can be created without belonging to any class. A lambda expression can be passed around as if it was an object and executed on demand.


1 Answers

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
}
like image 110
Jon Skeet Avatar answered Sep 23 '22 01:09

Jon Skeet