Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generic method to set the value of a property using expressions/lambda

I am trying to find a generic way to assign values to a property dictated by a lambda expression, look at the example code below, how would the signature for the ConverToEntities method look and how would it be called?

static void Main()
{
    List<long> ids = new List<long> {1, 2, 3};

    //Non generic way
    List<Data> dataItems = ids.ConvertToDataItems();

    //Generic attempt!!
    List<Data> differntDataItems =
        ids.ConvertToEntities<Data>( p => p.DataId );
}

public class Data
{
    public long DataId;
    public string Name;
}

public static class ExtensionMethods
{
    public static List<Data> ConvertToDataItems(this List<long> dataIds)
    {
        return dataIds.Select(p => new Data { DataId = p }).ToList();
    }

    public static List<T> ConvertToEntities<TProp>(
        this List<long> entities, Func<TProp> lambdaProperty )
    {
        return entities.Select(p => new T {lambdaProperty} ).ToList();
    }
}
like image 511
Ricky Gummadi Avatar asked Feb 14 '12 01:02

Ricky Gummadi


2 Answers

Ok. The closest I could get was this :

 class Program
    {
        static void Main(string[] args)
        {
            List<long> ids = new List<long> { 1, 2, 3 };

            //Non generic way
            List<Data> dataItems = ids.ConvertToDataItems();

            //Generic attempt!!

            Func<long, Data> selector = (p => new Data { DataId = p });
            List<Data> differntDataItems = ids.ConvertToEntities<Data>(selector);
        }
    }

    public class Data
    {
        public long DataId;
        public string Name;
    }

    public static class ExtensionMethods
    {
        public static List<Data> ConvertToDataItems(this List<long> dataIds)
        {
            return dataIds.Select(p => new Data { DataId = p }).ToList();
        }

        public static List<TProp> ConvertToEntities<TProp>(this List<long> entities, Func<long, TProp> selector)
        {
            return entities.Select(selector).ToList();
        }
    }

This works.

I have the feeling you got urself a little confused with what you actually want as the return type. It would be cool to be able to specify what we want in the method call or smth. For example:

    public static List<TProp> ConvertToEntities<T, TProp>(List<T> entities, Func<T, TProp> selector)
    {
        return entities.Select(selector).ToList();
    }

This provides us more flexibility on the return type. But since we are doing this using extensions, I assume this is impractical because we need to know what type we are extending:

this List<long> entities,

Nice question.

EDIT Code suggestion fix.

like image 99
Orkun Ozen Avatar answered Sep 26 '22 02:09

Orkun Ozen


You can do something like this, but it's not as simple or nice. The lambda p => p.DataId gives you the get accessor of the property. You could use Expressions to get the setter, but it's probably better to use the setter directly in the lambda:

List<Data> differntDataItems =
    ids.ConvertToEntities<long, Data>((p, i) => p.DataId = i);

The implementation would look like this:

public static List<T> ConvertToEntities<TProp, T>(
    this List<TProp> dataIds, Action<T, TProp> lambdaProperty)
    where T : new()
{
    return dataIds.Select(
        p =>
        {
            var result = new T();
            lambdaProperty(result, p);
            return result;
        }).ToList();
}
like image 45
svick Avatar answered Sep 23 '22 02:09

svick